-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstructBinaryTreeFromInorderAndPostOrderTraversal`
61 lines (52 loc) · 1.41 KB
/
ConstructBinaryTreeFromInorderAndPostOrderTraversal`
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/************************************************************
Following is the TreeNode class structure
class TreeNode<T>
{
public:
T data;
TreeNode<T> *left;
TreeNode<T> *right;
TreeNode(T data)
{
this -> data = data;
left = NULL;
right = NULL;
}
};
************************************************************/
TreeNode<int> * helper(vector<int> & in,int ins,int ine,vector<int> & po,int pos,int poe)
{
if(ins>ine)
return NULL;
int rootdata=po[poe];
TreeNode<int> * root=new TreeNode<int>(rootdata);
int rootindex;
for(int i=ins;i<=ine;i++)
{
if(in[i]==rootdata)
{
rootindex=i;
break;
}
}
int rpoe=poe-1;
int lpos=pos;
int line=rootindex-1;
int lins=ins;
int rins=rootindex+1;
int rine=ine;
int lpoe;
int rpos;
rpos=rpoe-(rine-rins);
lpoe=rpos-1;
root->left=helper(in,lins,line,po,lpos,lpoe);
root->right=helper(in,rins,rine,po,rpos,rpoe);
return root;
}
TreeNode<int>* getTreeFromPostorderAndInorder(vector<int>& postOrder, vector<int>& inOrder)
{
int n=inOrder.size();
int st=0;
int ed=n-1;
return helper(inOrder,st,ed,postOrder,st,ed);
}