-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaximumDepthOfNaryTree.java
More file actions
49 lines (38 loc) · 1.46 KB
/
Copy pathMaximumDepthOfNaryTree.java
File metadata and controls
49 lines (38 loc) · 1.46 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
49
// Given a n-ary tree, find its maximum depth.
// The maximum depth is the number of nodes along
// the longest path from the root node down to the farthest leaf node.
// See: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/
package leetcode.tree;
import java.util.ArrayList;
import java.util.Arrays;
import leetcode.util.tree.Node;
public class MaximumDepthOfNaryTree {
private int maxDepth = 0;
public int maxDepth(Node root) {
helper(root, 1);
return maxDepth;
}
private void helper(Node root, int depth) {
if (root == null) return;
if (root.children.isEmpty() && depth > maxDepth) {
maxDepth = depth;
}
if (root.children != null) {
for (Node child : root.children) {
helper(child, depth + 1);
}
}
}
public static void main(String[] args) {
Node n1 = new Node(1, new ArrayList<>());
Node n2 = new Node(2, new ArrayList<>());
Node n3 = new Node(3, new ArrayList<>());
Node n4 = new Node(4, new ArrayList<>());
Node n5 = new Node(5, new ArrayList<>());
Node n6 = new Node(6, new ArrayList<>());
n1.children = Arrays.asList(new Node[] { n3, n2, n4 });
n3.children = Arrays.asList(new Node[] { n5, n6 });
System.out.println(new MaximumDepthOfNaryTree().maxDepth(n1));
System.out.println(new MaximumDepthOfNaryTree().maxDepth(null));
}
}