@@ -103,6 +103,41 @@ function MaxDepthOfBinaryTree() {
103
103
#### Code in Different Languages
104
104
105
105
<Tabs >
106
+ <TabItem value =" JavaScript " label =" JavaScript " default >
107
+ <SolutionAuthor name =" @Vipullakum007 " />
108
+ ``` javascript
109
+ function maxDepth (root ) {
110
+ if (! root) return 0 ;
111
+ const maxLeft = maxDepth (root .left );
112
+ const maxRight = maxDepth (root .right );
113
+ return Math .max (maxLeft, maxRight) + 1 ;
114
+ }
115
+ ```
116
+
117
+ < / TabItem>
118
+ < TabItem value= " TypeScript" label= " TypeScript" >
119
+ < SolutionAuthor name= " @Vipullakum007" / >
120
+ ` ` ` typescript
121
+ class TreeNode {
122
+ val: number;
123
+ left: TreeNode | null;
124
+ right: TreeNode | null;
125
+ constructor(val: number) {
126
+ this.val = val;
127
+ this.left = null;
128
+ this.right = null;
129
+ }
130
+ }
131
+
132
+ function maxDepth(root: TreeNode | null): number {
133
+ if (!root) return 0;
134
+ const maxLeft: number = maxDepth(root.left);
135
+ const maxRight: number = maxDepth(root.right);
136
+ return Math.max(maxLeft, maxRight) + 1;
137
+ }
138
+ ` ` `
139
+
140
+ < / TabItem>
106
141
< TabItem value= " Java" label= " Java" default>
107
142
< SolutionAuthor name= " @Vipullakum007" / >
108
143
` ` ` java
@@ -168,11 +203,118 @@ Another approach to find the maximum depth of a binary tree is to use breadth-fi
168
203
169
204
#### Implementation
170
205
206
+ ` ` ` jsx live
207
+ function MaxDepthOfBinaryTree() {
208
+ class TreeNode {
209
+ constructor(val) {
210
+ this.val = val;
211
+ this.left = null;
212
+ this.right = null;
213
+ }
214
+ }
171
215
216
+ // Creating the binary tree
217
+ const root = new TreeNode(3);
218
+ root.left = new TreeNode(9);
219
+ root.right = new TreeNode(20);
220
+ root.right.left = new TreeNode(15);
221
+ root.right.right = new TreeNode(7);
222
+
223
+ const maxDepth = function (root) {
224
+ if (!root) return 0;
225
+
226
+ let depth = 0;
227
+ const queue = [root];
228
+
229
+ while (queue.length) {
230
+ const size = queue.length;
231
+ for (let i = 0; i < size; i++) {
232
+ const node = queue.shift();
233
+ if (node.left) queue.push(node.left);
234
+ if (node.right) queue.push(node.right);
235
+ }
236
+ depth++;
237
+ }
238
+
239
+ return depth;
240
+ };
241
+
242
+ const result = maxDepth(root);
243
+ return (
244
+ <div>
245
+ <p>
246
+ <b>Binary Tree:</b> {JSON.stringify(root)}
247
+ </p>
248
+ <p>
249
+ <b>Maximum Depth:</b> {result}
250
+ </p>
251
+ </div>
252
+ );
253
+ }
254
+ ` ` `
172
255
173
256
#### Code in Different Languages
174
257
175
258
< Tabs>
259
+ < TabItem value= " JavaScript" label= " JavaScript" default>
260
+ < SolutionAuthor name= " @Vipullakum007" / >
261
+ ` ` ` javascript
262
+ function maxDepth(root) {
263
+ if (!root) return 0;
264
+
265
+ let depth = 0;
266
+ const queue = [root];
267
+
268
+ while (queue.length) {
269
+ depth++;
270
+ const levelSize = queue.length;
271
+ for (let i = 0; i < levelSize; ++i) {
272
+ const node = queue.shift();
273
+ if (node.left) queue.push(node.left);
274
+ if (node.right) queue.push(node.right);
275
+ }
276
+ }
277
+
278
+ return depth;
279
+ }
280
+ ` ` `
281
+
282
+ < / TabItem>
283
+ < TabItem value= " TypeScript" label= " TypeScript" >
284
+ < SolutionAuthor name= " @Vipullakum007" / >
285
+ ` ` ` typescript
286
+ class TreeNode {
287
+ val: number;
288
+ left: TreeNode | null;
289
+ right: TreeNode | null;
290
+ constructor(val: number) {
291
+ this.val = val;
292
+ this.left = null;
293
+ this.right = null;
294
+ }
295
+ }
296
+
297
+ function maxDepth(root: TreeNode | null): number {
298
+ if (!root) return 0;
299
+
300
+ let depth = 0;
301
+ const queue: TreeNode[] = [root];
302
+
303
+ while (queue.length) {
304
+ depth++;
305
+ const levelSize = queue.length;
306
+ for (let i = 0; i < levelSize; ++i) {
307
+ const node = queue.shift()!;
308
+ if (node.left) queue.push(node.left);
309
+ if (node.right) queue.push(node.right);
310
+ }
311
+ }
312
+
313
+ return depth;
314
+ }
315
+ ` ` `
316
+
317
+ < / TabItem>
176
318
< TabItem value= " Java" label= " Java" default>
177
319
< SolutionAuthor name= " @Vipullakum007" / >
178
320
` ` ` java
0 commit comments