题解 | #实现二叉树先序,中序和后序遍历#

实现二叉树先序,中序和后序遍历

http://www.nowcoder.com/practice/a9fec6c46a684ad5a3abd4e365a9d362

/**
 * struct TreeNode {
 *    int val;
 *    struct TreeNode *left;
 *    struct TreeNode *right;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param root TreeNode类 the root of binary tree
     * @return int整型vector<vector<>>
     */
    vector<int> pre;
    vector<int> in;
    vector<int> post;
    void preorder(TreeNode* root){
        if(!root)
            return;
        pre.push_back(root->val);
        preorder(root->left);
        preorder(root->right);
    }
    void inorder(TreeNode* root){
        if(!root)
            return;
        inorder(root->left);
        in.push_back(root->val);
        inorder(root->right);
    }
    void postorder(TreeNode* root){
        if(!root)
            return;
        postorder(root->left);
        postorder(root->right);
        post.push_back(root->val);
    }

    vector<vector<int>> threeOrders(TreeNode* root) {
        // write code here
        vector<vector<int>> res;
        preorder(root);
        inorder(root);
        postorder(root);
        res.push_back(pre);
        res.push_back(in);
        res.push_back(post);
        return res;
    }
};
全部评论

相关推荐

不愿透露姓名的神秘牛友
今天 12:02
ssob上原来真有BOSS啊
硫蛋蛋:这种也是打工的,只不是是给写字楼房东打工
点赞 评论 收藏
分享
见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务