-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec63_treeques.cpp
180 lines (129 loc) · 3.18 KB
/
lec63_treeques.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Height of a tree
/*
int height(struct Node *node)
{
if (node == NULL)
return 0;
int left = height(node->left);
int right = height(node->right);
int ans = max(left, right) + 1;
return ans;
}
*/
// Diameter of a tree (APPROACH #1)
/*
class Solution
{
private:
int height(Node *root)
{
if (root == NULL)
return 0;
int left = height(root->left);
int right = height(root->right);
int ans = max(left, right) + 1;
return ans;
}
public:
// Function to return the diameter of a Binary Tree.
int diameter(Node *root)
{
if (root == NULL)
return 0;
int op1 = diameter(root->left);
int op2 = diameter(root->right);
int op3 = height(root->left) + 1 + height(root->right);
int ans = max(op3, max(op1, op2));
return ans;
}
};
*/
// Diameter of tree (APPROACH #2)
/*
class Solution {
public:
pair <int,int> diameterfast(Node* root){
if(root==NULL){
pair <int,int> p = make_pair(0,0);
return p;
}
pair<int,int> left = diameterfast(root->left);
pair<int,int> right = diameterfast(root->right);
int op1 = left.first;
int op2 = right.first;
int op3 = left.second + right.second + 1;
pair<int,int> ans;
ans.first = max(op3,max(op1,op2));
ans.second = max(left.second,right.second)+1;
return ans;
}
// Function to return the diameter of a Binary Tree.
int diameter(Node* root) {
return diameterfast(root).first;
}
};
*/
// Check Balanced tree (APPROACH #1)
/*
class Solution
{
private:
int height(Node *root)
{
if (root == NULL)
return 0;
int left = height(root->left);
int right = height(root->right);
int ans = max(left, right) + 1;
return ans;
}
public:
// Function to check whether a binary tree is balanced or not.
bool isBalanced(Node *root)
{
// base case
if (root == NULL)
return true;
bool left = isBalanced(root->left);
bool right = isBalanced(root->right);
bool diff = abs(height(root->left) - height(root->right)) <= 1;
if (left && right && diff)
return true;
else
return false;
}
};
*/
// Check Balanced tree (APPROACH #2)
/*
class Solution
{
public:
pair<bool, int> balfast(Node *root)
{
if (root == NULL)
{
pair<bool, int> p = make_pair(true, 0);
return p;
}
pair<bool, int> left = balfast(root->left);
pair<bool, int> right = balfast(root->right);
bool leftans = left.first;
bool rightans = right.first;
bool diff = abs(left.second - right.second) <= 1;
pair<bool, int> ans;
ans.second = max(left.second, right.second) + 1;
if (leftans && rightans && diff)
ans.first = true;
else
ans.first = false;
return ans;
}
public:
// Function to check whether a binary tree is balanced or not.
bool isBalanced(Node *root)
{
return balfast(root).first;
}
};
*/