美文网首页
construct-binary-tree-from-inord

construct-binary-tree-from-inord

作者: DaiMorph | 来源:发表于2019-05-24 23:22 被阅读0次
    class Solution {
    public:
        TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
            return create(inorder,postorder,0,inorder.size()-1,0,postorder.size()-1);
        }
        TreeNode *create(vector<int>in,vector<int>post,int inL,int inR,int postL,int postR)
        {
            if(postL>postR)return NULL;
            TreeNode*root=new TreeNode(post[postR]);
            int k;
            for(k=inL;k<=inR;k++)
            {
                if(in[k]==root->val)break;
            }
            int numleft=k-inL;
            root->left=create(in,post,inL,k-1,postL,postL+numleft-1);
            root->right=create(in,post,k+1,inR,postL+numleft,postR-1);
            return root;
        }
    };
    

    相关文章

      网友评论

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

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