-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Diameter_of_BT.cpp
113 lines (86 loc) · 2.22 KB
/
Diameter_of_BT.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
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* left;
Node* right;
Node(int val)
{
data = val;
left = NULL;
right = NULL;
}
};
void printBT(const std::string& prefix, const Node* node, bool isLeft)
{
if (node != nullptr)
{
std::cout << prefix;
std::cout << (isLeft ? "|--" : "L--");
std::cout << node->data << std::endl;
printBT(prefix + (isLeft ? "| " : " "), node->right, true);
printBT(prefix + (isLeft ? "| " : " "), node->left, false);
}
}
void printBT(const Node* node)
{
printBT("", node, false);
}
int Height(Node* node)
{
if(node==NULL)
return 0;
return max(Height(node->left),Height(node->right)) + 1;
}
int diameterBT(Node* root) // O(N^2)
{
if(root==NULL)
return 0;
int lefth = Height(root->left);
int righth = Height(root->right);
int currdia = lefth + righth + 1;
int leftdia = diameterBT(root->left);
int rightdia = diameterBT(root->right);
return max(currdia, max(leftdia, rightdia));
}
int modifiedDiameterBT(Node* root, int* height)
{
if(root==NULL)
{
*height = 0;
return 0;
}
int lh =0, rh =0;
int ldia = modifiedDiameterBT(root->left, &lh);
int rdia = modifiedDiameterBT(root->right, &rh);
int currdia = lh + rh +1;
*height = max(lh,rh) + 1;
return max(currdia, max(ldia, rdia));
}
pair<int,int> modifiedBT2(Node* root)
{
if(root==NULL)
{
return {0,0};
}
pair<int,int> leftdia = modifiedBT2(root->left);
pair<int,int> rightdia = modifiedBT2(root->right);
int op1 = leftdia.first;
int op2 = rightdia.first;
int op3 = leftdia.second + rightdia.second + 1;
return {max(op1, max(op2,op3)), max(leftdia.second,rightdia.second)+1};
}
int main()
{
Node* root = new Node(1);
root->left = new Node(2);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->left->left->left = new Node(6);
root->left->left->right = new Node(7);
int height = 0;
cout<<diameterBT(root)<<endl;
cout<<modifiedDiameterBT(root, &height)<<endl;
return 0;
}