construct-binary-tree-from-inord
2019-05-24 本文已影响0人
DaiMorph
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;
}
};