-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstruct_tree_from_inorder_postorder.cpp
More file actions
60 lines (51 loc) · 1.24 KB
/
Copy pathconstruct_tree_from_inorder_postorder.cpp
File metadata and controls
60 lines (51 loc) · 1.24 KB
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
#include<iostream>
#include<vector>
using namespace std;
struct Node
{
Node* left;
Node* right;
int data;
Node(int val)//constructor
{
data=val;
left=right=NULL;
}
};
int get_index(vector<int>&inorder,int target,int start,int ed)//find index
{
for(int i=start;i<=ed;i++)
{
if(inorder[i]==target)
{
return i;
}
}
return -1;
}
Node* tree(vector<int>&postorder,vector<int>&inorder,int ps,int* pe,int is,int ie)//function for construct tree
{
if(is>ie || *pe <0)return NULL;//base condition
Node* root=new Node(postorder[*pe]);
int target=postorder[(*pe)--];
int get=get_index(inorder,target,is,ie);
root->right=tree(postorder,inorder,ps,pe,get+1,ie);
root->left=tree(postorder,inorder,ps,pe,is,get-1);
return root;
}
void print_nodes(Node* root)
{
if(root==NULL)return;
print_nodes(root->left);
cout<<root->data<<" ";
print_nodes(root->right);
}
int main()
{
vector<int>postorder={5,6,4,9,2,3};//left right,root
vector<int>inorder={5,4,6,3,2,9};//left root tight
int n=postorder.size()-1;
Node* root= tree(postorder,inorder,0,&n,0,5);//we need to pass by refrence
print_nodes(root);
return 0;
}