-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstruct Binary Tree from Inorder and Postorder.cpp
77 lines (67 loc) · 1.85 KB
/
Construct Binary Tree from Inorder and Postorder.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Method - 1
Recursion + Linear Search
Time complexity - O(N^2)
Space complexity- O(H)
*/
class Solution
{
public:
Node* help(int* in,int* post,int inStart,int inEnd,int postStart,int postEnd)
{
//base case
if(inStart>inEnd)
return NULL;
//recursive calls
//and small calculation
Node* root=new Node(post[postEnd]);
int ind=-1;
for(int i=inStart;i<=inEnd;i++)
{
if(in[i]==post[postEnd])
{
ind=i;
break;
}
}
int leftSize=ind-inStart;
int rightSize=inEnd-ind;
root->left=help(in,post,inStart,ind-1,postStart,postStart+leftSize-1);
root->right=help(in,post,ind+1,inEnd,postEnd-rightSize,postEnd-1);
return root;
}
Node *buildTree(int in[], int post[], int n) {
return help(in,post,0,n-1,0,n-1);
}
};
/*
Method - 2
Recursion + Unordered_map
Time complexity - O(N)
Space complexity- O(N+H)
*/
class Solution
{
public:
Node* help(int* in,int* post,unordered_map<int,int>& mp,int inStart,int inEnd,int postStart,int postEnd)
{
//base case
if(inStart>inEnd)
return NULL;
//recursive calls
//and small calculation
Node* root=new Node(post[postEnd]);
int ind=mp[post[postEnd]];
int leftSize=ind-inStart;
int rightSize=inEnd-ind;
root->left=help(in,post,mp,inStart,ind-1,postStart,postStart+leftSize-1);
root->right=help(in,post,mp,ind+1,inEnd,postEnd-rightSize,postEnd-1);
return root;
}
Node *buildTree(int in[], int post[], int n) {
unordered_map<int,int> mp;
for(int i=0;i<n;i++)
mp[in[i]]=i;
return help(in,post,mp,0,n-1,0,n-1);
}
};