美文网首页LeetCode刷题
construct-binary-tree-from-inord

construct-binary-tree-from-inord

作者: 美不胜收oo | 来源:发表于2018-12-16 22:42 被阅读0次

    描述:

    Given inorder and postorder traversal of a tree, construct the binary tree.

    Note:
    You may assume that duplicates do not exist in the tree.

    思想:

    就按照中序遍历和后序遍历建立二叉树

    C++代码:

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
            if(inorder.size() == 0)
                return NULL;
            int size = postorder.size();
            TreeNode* root = new TreeNode(postorder[size-1]);
            postorder.erase(postorder.end()-1);
            if(postorder.size() == 0)
                return root;
            vector<int>::iterator result = find(inorder.begin(),inorder.end(),postorder[size-1]);
            vector<int> inleft,inright,postleft,postright;
            inleft.assign(inorder.begin(),result);
            inright.assign(result+1, inorder.end());
            postleft.assign(postorder.begin(), postorder.begin()+inleft.size());
            postright.assign(postorder.begin()+inleft.size(), postorder.end());
            
            root->left = buildTree(inleft, postleft);
            root->right = buildTree(inright, postright);
            
            return root;
            
        }
    };
    

    相关文章

      网友评论

        本文标题:construct-binary-tree-from-inord

        本文链接:https://www.haomeiwen.com/subject/uorfkqtx.html