和上面的题目相似 思路也是类似
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); }
Construct binary tree form postorder and inorder--LeetCode
原文地址:http://blog.csdn.net/yusiguyuan/article/details/44746939