-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuild-Tree-from-Preorder-and-Inorde.cpp
133 lines (120 loc) · 2.85 KB
/
Build-Tree-from-Preorder-and-Inorde.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node *left;
Node *right;
Node(int val){
data = val;
left = NULL;
right = NULL;
}
};
int search(int inOrder[], int start, int end, int curr){
for(int i = start; i <= end; i++){
if(inOrder[i] == curr){
return i;
}
}
return -1;
}
int heightOfTree(Node *root){
if(root == NULL){
return 0;
}
int lh = heightOfTree(root->left);
int rh = heightOfTree(root->right);
return max(lh, rh) + 1;
}
int diameterOfTree(Node *root){
if(root == NULL){
return 0;
}
int lh = heightOfTree(root->left);
int rh = heightOfTree(root->right);
int currDiameter = lh + rh + 1;
int leftDiameter = diameterOfTree(root->left);
int rightDiameter = diameterOfTree(root->right);
return max(currDiameter, max(leftDiameter, rightDiameter));
}
int maxpathSum(Node *root, int &res){
if(root == NULL){
return 0;
}
int lh = maxpathSum(root->left, res);
int rh = maxpathSum(root->right, res);
int temp = max(max(lh, rh) + root->data, root->data);
int ans = max(temp, lh + rh + root->data);
res = max(res, ans);
return temp;
}
Node* buildTree(int preOrder[], int inOrder[], int start, int end){
if(start > end){
return NULL;
}
static int idx = 0;
int curr = preOrder[idx];
idx++;
Node *node = new Node(curr);
int pos = search(inOrder, start, end, curr);
if(start == end){
return node;
}
node->left = buildTree(preOrder, inOrder, start, pos-1);
node->right = buildTree(preOrder, inOrder, pos+1, end);
return node;
}
Node* buildTreePost(int postOrder[], int inOrder[], int start, int end){
if(start > end){
return NULL;
}
static int idx = 6;
int curr = postOrder[idx];
idx--;
Node *node = new Node(curr);
int pos = search(inOrder, start, end, curr);
if(start == end){
return node;
}
node->right = buildTreePost(postOrder, inOrder, pos+1, end);
node->left = buildTreePost(postOrder, inOrder, start, pos-1);
return node;
}
nodeToRootPath(Node *root, int key, vector<int> &path){
if(root == NULL){
return false;
}
if(root->data == key){
path.push_back(root->data);
return true;
}
bool left = nodeToRootPath(root->left, key, path);
bool right = nodeToRootPath(root->right, key, path);
if(left || right){
path.push_back(root->data);
return true;
}
return false;
}
void preOrderr(Node *root){
if (root == NULL){
return;
}
cout<<root->data << " ";
preOrderr(root->left);
preOrderr(root->right);
}
int main(){
int preOrder[] = {1, 2, 4, 5, 3, 6, 7};
int inOrder[] = {4, 2, 5, 1, 6, 3, 7};
int postOrder[] = {4, 5, 2, 6, 7, 3, 1};
Node *root = buildTree(preOrder, inOrder, 0, 6);
Node *root2 = buildTreePost(postOrder, inOrder, 0, 6);
preOrderr(root);
cout<<endl;
cout<<diameterOfTree(root);
cout<<endl;
int res = INT_MIN;
maxpathSum(root, res);
return 0;
}