67.二叉树的中序遍历

来源:互联网 发布:对万方数据库的评价 编辑:程序博客网 时间:2024/05/02 04:21

题目:给出一棵二叉树,返回其中序遍历。


样例:

给出二叉树 {1,#,2,3},

   1    \     2    /   3

返回 [1,3,2].

代码:

class Solution {    /**     * @param root: The root of binary tree.     * @return: Inorder in vector which contains node values.     */     void in(vector<int> &v,TreeNode *root)     {         if(root==NULL) return;         else {             in(v,root->left);             v.push_back(root->val);             in(v,root->right);         }     }public:    vector<int> inorderTraversal(TreeNode *root) {        // write your code here        vector<int> v;        in(v,root);        return v;    }};
感想:同前序遍历一样。

1 0
原创粉丝点击