二叉树的中序遍历

来源:互联网 发布:易石软件怎样用 编辑:程序博客网 时间:2024/06/06 10:46

给出一棵二叉树,返回其中序遍历

样例

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

   1    \     2    /   3

返回 [1,3,2].

/** * Definition of TreeNode: * class TreeNode { * public: *     int val; *     TreeNode *left, *right; *     TreeNode(int val) { *         this->val = val; *         this->left = this->right = NULL; *     } * } */class Solution {public:    /*     * @param root: A Tree     * @return: Inorder in ArrayList which contains node values.     */    vector<int> inorderTraversal(TreeNode * root) {        // write your code here        vector<int> temp;        if(root == NULL)          return temp;        inorder(root,temp);        return temp;    }        void inorder(TreeNode *root,vector<int> &path)    {        if(root != NULL)        {            inorder(root->left,path);            path.push_back(root->val);            inorder(root->right,path);        }        if(root == NULL)         return ;    }};


原创粉丝点击