-
Notifications
You must be signed in to change notification settings - Fork 3
/
16th Aug_BT and BST Questions.java
289 lines (141 loc) · 4.81 KB
/
16th Aug_BT and BST Questions.java
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
Date : 16th August 2022
Mentor: DEVANG SHARMA
Batch: March Batch (Advanced DSA) - Challenger
Agenda:
- Trees
- Binary Search Trees
- Interview Questions on BT and BST
Trees- Binary Trees
https://leetcode.com/problems/minimum-depth-of-binary-tree/: DONE
https://leetcode.com/problems/maximum-depth-of-n-ary-tree/: DONE
https://leetcode.com/problems/balanced-binary-tree/: DONE
LC - Premium : https://leetcode.com/problems/binary-tree-upside-down/
Trees- Binary Search Trees
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree - IMP
https://leetcode.com/problems/minimum-absolute-difference-in-bst: DONE
https://leetcode.com/problems/search-in-a-binary-search-tree: DONE
https://leetcode.com/problems/range-sum-of-bst: DONE
"Please Type 'Hi' in the Chat Box if you have joined and Can See this Screen".
Hard: BST Questions
Q: [LC-110] Balanced Binary Tree
https://leetcode.com/problems/balanced-binary-tree/
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as:
a binary tree in which the left and right subtrees of "every node" differ in height by no more than 1.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: true
Example 2:
Input: root = [1,2,2,3,3,null,null,4,4]
Output: false
Example 3:
Input: root = []
Output: true
Constraints:
The number of nodes in the tree is in the range [0, 5000].
-104 <= Node.val <= 104
public boolean isBalanced(TreeNode root)
{
}
CODE:
// T - O(N), S - O(N)
class Solution {
public boolean result = true;
public boolean isBalanced(TreeNode root)
{
maxDepth(root);
return result;
}
public int maxDepth(TreeNode root)
{
if (!result)
return 0;
if (root == null)
return 0;
int leftHeight = maxDepth(root.left);
int rightHeight = maxDepth(root.right);
// Not Height Balanced: diff > 1
if (Math.abs(leftHeight - rightHeight) > 1)
result = false;
// Height Balanced: diff <= 1, recur for next node
return 1 + Math.max(leftHeight, rightHeight);
}
}
[IMP] Q: [LC-530] Minimum Absolute Difference in BST
https://leetcode.com/problems/minimum-absolute-difference-in-bst
Given the root of a Binary Search Tree (BST),
return the minimum absolute difference between the values of any two different nodes in the tree.
Example 1:
Input: root = [4,2,6,1,3]
Output: 1
Example 2:
Input: root = [1,0,48,null,null,12,49]
Output: 1
Constraints:
The number of nodes in the tree is in the range [2, 104].
0 <= Node.val <= 105
public int getMinimumDifference(TreeNode root) {
}
Solution:
Inorder Traversal of BST --> Sorted List of Values
Minimum Absolute Difference in Sorted Array: Between Adjacent Values
Maximum Absolute Difference in Sorted Array: Between First and Last Values
[4,2,6,1,3]
After Sorting: [1 2 3 4 6]
Min Diff = 1,2 = 2,3 = 3,4 = 1
Max Diff = 1,6 = 5
Note:
No Need to store Inorder Traversal,
Just keep a variable to store minimum absolute Adjacent difference in Inorder Traversal
Approach:
(1) Inorder Traversal
(2) Difference of Adjacent Values
(3) Minimum Adjacent Value Difference
Two Ways:
(1) Store Inorder Traversal in an ArrayList
Find the Min Adjacent Values Difference
TC - O(N), SC - O(N)
(2) Dont Store Inorder Traversal in an ArrayList,
Use a Variable
Find the Min Adjacent Values Difference
TC - O(N), SC - O(1)
Edge Case:
[0, null, 2236, 1277, 2776, 519]
0
_ 2236
1277 2776
519
Inorder Traversal:
[0 519 1277 2236 2776]
Correct OP: 519
curr > 0: curr will never become 0
diff = 1277-519, 2236-1277, 2776-2236
Min Value = 2776-2236 = 540
OP: 540
// T - O(N), S- O(N)
class Solution {
// Global because Recursive function will reuse same value everytime
int min_diff = Integer.MAX_VALUE; // Ans: Min Diff between any 2 Nodes
int curr_diff = -1; // Min Diff between any 2 ADJACENT Nodes in Inorder Tarversal
public int getMinimumDifference(TreeNode root)
{
// Inorder: Left - Root - Right
// Recur on Left
if (root.left!=null)
getMinimumDifference(root.left);
if (curr_diff >= 0)
min_diff = Math.min(min_diff, root.val - curr_diff);
// Hold previous value
// root.val - curr_diff -> curr - prev value
curr_diff = root.val;
// Recur on Right
if (root.right!=null)
getMinimumDifference(root.right);
return min_diff;
}
}
HW:
LC - 700
https://leetcode.com/problems/search-in-a-binary-search-tree:
LC - 938
https://leetcode.com/problems/range-sum-of-bst: