Maximum Depth of N-ary Tree LeetCode solution in Java

1
class Solution {
2
public int maxDepth(Node root) {
3
if (root == null) return 0;
4
int[] max = new int[] {0};
5
dfs(root, 1, max);
6
return max[0];
7
}
8

9
public static void dfs(Node root, int depth, int[] max) {
10
if (depth > max[0]) max[0] = depth;
11
if (root == null) {
12
return;
13
}
14
++depth;
15
for (Node n : root.children) dfs(n, depth, max);
16
}
17
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0