Maximum Depth of N-ary Tree LeetCode solution in JavaScript

1
/**
2
* // Definition for a Node.
3
* function Node(val,children) {
4
* this.val = val;
5
* this.children = children;
6
* };
7
*/
8

9
/**
10
* @param {Node|null} root
11
* @return {number}
12
*/
13
var maxDepth = function (root) {
14
let max = 0;
15

16
if (!root) {
17
return max;
18
}
19

20
const search = (root, index) => {
21
max = Math.max(index, max);
22

23
if (root?.children && root?.children.length > 0) {
24
for (let i = 0; i < root.children.length; i++) {
25
search(root.children[i], index + 1);
26
}
27
}
28
};
29

30
search(root, 1);
31

32
return max;
33
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0