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

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

https://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,mid,post;

    void preOrder(TreeNode* root){
        if(root == NULL)
            return;
        pre.push_back(root->val);
        preOrder(root->left);
        preOrder(root->right);
    }

    void midOrder(TreeNode* root){
        if(root == NULL)
            return;
        midOrder(root->left);
        mid.push_back(root->val);
        midOrder(root->right);
    }

    void postOrder(TreeNode* root){
        if(root == NULL)
            return;
        postOrder(root->left);
        postOrder(root->right);
        post.push_back(root->val);
    }

    vector<vector<int> > threeOrders(TreeNode* root) {
        // write code here
        if(root != nullptr){
            preOrder(root);
            midOrder(root);
            postOrder(root);
        }
        vector<vector<int> >order = {pre,mid,post};
        return order;
    }
};

全部评论

相关推荐

把球:这个听过,你加了就会发现是字节的hr
点赞 评论 收藏
分享
吃不饱的肱二头肌很想退休:tnnd 我以为选妹子呢,亏我兴高采烈的冲进来😠
投递快手等公司10个岗位
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务