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

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

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> 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); 
    }    
};

全部评论
该牛油正在参与牛客写题解薅羊毛的活动,牛币,周边,京东卡超多奖品放送,活动进入倒计时!快来捡漏啦https://www.nowcoder.com/discuss/888949?source_id=profile_create_nctrack&channel=-1
点赞 回复 分享
发布于 2022-04-27 12:16

相关推荐

谁知道呢_:要掉小珍珠了,库库学三年,这个结果
点赞 评论 收藏
分享
04-11 23:51
门头沟学院 Java
坚定的芭乐反对画饼_许愿Offer版:人人都能过要面试干嘛,发个美团问卷填一下,明天来上班不就好了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务