根据一棵树的中序遍历与后序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:
3 / \ 9 20 / \ 15 7
递归的思想
后序的最后一个就是根节点,在中序里面找到这个节点,递归这个过程
可以申请临时vector,这样占内存比较多,但是不用调用子函数
直接在vector上求解,用左右指针进行处理,这样必须调用子函数buildTreeCoreFun把形参left,right放进去。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *buildTreeCoreFun(vector& inorder,int InorderLeft,int InorderRight,vector& postorder,int PostorderLeft,int PostorderRight)
{
if (inorder.size()==0)
return NULL;
TreeNode* tree=new TreeNode(postorder[PostorderRight]);
if (PostorderRight==PostorderLeft||InorderLeft==InorderRight)
return tree;
vector::iterator it=find(inorder.begin()+InorderLeft,inorder.begin()+InorderRight,tree->val);
int position=0;
if (it!=inorder.end())
position=distance(inorder.begin(),it);
int lenLeft=position-InorderLeft;
int lenRight=InorderRight-position;
if(lenLeft)
tree->left =buildTreeCoreFun(inorder,InorderLeft,InorderLeft+lenLeft-1,postorder,PostorderLeft,PostorderLeft+lenLeft-1);
if(lenRight)
tree->right =buildTreeCoreFun(inorder,InorderRight-lenRight+1,InorderRight,postorder,PostorderRight-lenRight,PostorderRight-1);
return tree;
}
TreeNode* buildTree(vector& inorder, vector& postorder) {
int InorderLeft,InorderRight;
int PostorderLeft,PostorderRight;
InorderLeft=PostorderLeft=0;
InorderRight=PostorderRight=postorder.size()-1;
return buildTreeCoreFun(inorder,InorderLeft,InorderRight,postorder,PostorderLeft,PostorderRight);
}
};
#include
#include
#include
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode *buildTreeCoreFun(vector& inorder,int InorderLeft,int InorderRight,vector& postorder,int PostorderLeft,int PostorderRight)
{
if (inorder.size()==0)
return NULL;
TreeNode* tree=new TreeNode(postorder[PostorderRight]);
if (PostorderRight==PostorderLeft||InorderLeft==InorderRight)
return tree;
vector::iterator it=find(inorder.begin()+InorderLeft,inorder.begin()+InorderRight,tree->val);
int position=0;
if (it!=inorder.end())
position=distance(inorder.begin(),it);
int lenLeft=position-InorderLeft;
int lenRight=InorderRight-position;
if(lenLeft)
tree->left =buildTreeCoreFun(inorder,InorderLeft,InorderLeft+lenLeft-1,postorder,PostorderLeft,PostorderLeft+lenLeft-1);
if(lenRight)
tree->right =buildTreeCoreFun(inorder,InorderRight-lenRight+1,InorderRight,postorder,PostorderRight-lenRight,PostorderRight-1);
return tree;
}
TreeNode* buildTree(vector& inorder, vector& postorder) {
int InorderLeft,InorderRight;
int PostorderLeft,PostorderRight;
InorderLeft=PostorderLeft=0;
InorderRight=PostorderRight=postorder.size()-1;
return buildTreeCoreFun(inorder,InorderLeft,InorderRight,postorder,PostorderLeft,PostorderRight);
}
int main()
{
/* int arr[5]={9,3,15,20,7};
int arr1[5]={9,15,7,20,3};
vector inorder(arr,arr+5);
vector posorder(arr1,arr1+5);*/
int arr[2]={2,1};
int arr1[2]={2,1};
vector inorder(arr,arr+2);
vector posorder(arr1,arr1+2);
TreeNode* root=buildTree(inorder,posorder);
int i=0;
return 0;
}