-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ559N-aryTree.java
More file actions
48 lines (40 loc) · 1.06 KB
/
Q559N-aryTree.java
File metadata and controls
48 lines (40 loc) · 1.06 KB
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
/*
@b-knd (jingru) on 07 August 2022 16:24:00
*/
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
int max = 0;
public int maxDepth(Node root) {
traverse(root, 0);
return max;
}
public void traverse(Node root, int curr){
if(root != null){
//increment current depth and recurse on children nodes
curr++;
if(root.children != null){
for(Node node: root.children){
traverse(node, curr);
}
}
}
//update max in every recursion call
max = Math.max(curr, max);
}
}
//Runtime: 1 ms, faster than 83.49% of Java online submissions for Maximum Depth of N-ary Tree.
//Memory Usage: 44.5 MB, less than 29.63% of Java online submissions for Maximum Depth of N-ary Tree.