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

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

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

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

class Solution {
public:
    vector<int> a,b,c;
     
    vector<vector<int> > threeOrders(TreeNode* root) {
        // write code here
        vector<vector<int>> ans;
        preOrder(root);
        inOrder(root);
        postOrder(root);
        ans.push_back(a);
        ans.push_back(b);
        ans.push_back(c);
        return ans;
    }
     
    void preOrder(TreeNode* root)
    {
        if(root==NULL) return;
        a.push_back(root->val);
        preOrder(root->left);
        preOrder(root->right);
    }
    void inOrder(TreeNode* root)
    {
        if(root==NULL) return;
        inOrder(root->left);
        b.push_back(root->val);        
        inOrder(root->right);
    }
    void postOrder(TreeNode* root)
    {
        if(root==NULL) return;
        postOrder(root->left);               
        postOrder(root->right);
        c.push_back(root->val); 
    }    
};

全部评论

相关推荐

10-10 16:30
济宁学院 Java
一表renzha:面试官:蓝桥杯三等奖?你多去两次厕所都能拿二等吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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