Construct binary tree form postorder and inorder--LeetCode

来源:互联网 发布:美化照片软件 编辑:程序博客网 时间:2024/06/05 05:14

和上面的题目相似 思路也是类似

Tree* helpersecond(vector<int>& inorder,int in_begin,int in_end,vector<int>& post,int post_begin,int post_end){Tree* root =NULL;int mid;int i;if(in_begin > in_end){return NULL;}else{for(i=in_begin;i<=in_end;i++)if(inorder[i] == post[post_end])break;if(i > in_end)return NULL;root = new Tree;root->value = post[post_end];root->left = helpersecond(inorder,in_begin,i-1,post,post_begin+i,post_end-1);root->right  =helpersecond(inorder,i+1,in_end,post,post_begin,post_begin+i-1);}}Tree* createBinTree(vector<int>& inorder,vector<int>& post) {if(inorder.size()==0 || post.size()==0)return NULL;return helpersecond(inorder,0,inorder.size()-1,post,0,post.size()-1);}


0 0
原创粉丝点击