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

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

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> preVec;
    vector<int> inVec;
    vector<int> postVec;
    vector<vector<int> > vec;
    void preOrder(TreeNode* root)
    {
        if(root)
        {
            preVec.push_back(root->val);
            preOrder(root->left);
            preOrder(root->right);
        }
    }

    void inOrder(TreeNode* root)
    {
        if(root)
        {
            inOrder(root->left);
            inVec.push_back(root->val);
            inOrder(root->right);
        }
    }

    void postOrder(TreeNode* root)
    {
        if(root)
        {
            postOrder(root->left);
            postOrder(root->right);
            postVec.push_back(root->val);
        }
    }

    vector<vector<int> > threeOrders(TreeNode* root) {
        preOrder(root);
        inOrder(root);
        postOrder(root);
        vec.push_back(preVec);
        vec.push_back(inVec);
        vec.push_back(postVec);
        return vec;
    }
};
全部评论

相关推荐

牛客37185681...:马德,我感觉这是我面过最恶心的公司,一面是两个女hr,说什么实习前几个月属于试用期,试用期过了才能转成正式实习生,我***笑了,问待遇就是不说,问能不能接受全栈,沙币公司
如果可以选,你最想去哪家...
点赞 评论 收藏
分享
09-18 12:13
已编辑
门头沟学院 产品经理
火猴大圣:不要委屈自己,进去看一看 。。。 这样你才能彻底死心
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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