Skip to content

Commit 03a8950

Browse files
authored
Create 559. Maximum Depth of N-ary Tree
1 parent e85a6c1 commit 03a8950

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

559. Maximum Depth of N-ary Tree

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
// Given a n-ary tree, find its maximum depth.
3+
// The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
4+
*/
5+
6+
/*
7+
// Definition for a Node.
8+
class Node {
9+
public int val;
10+
public List<Node> children;
11+
12+
public Node() {}
13+
14+
public Node(int _val,List<Node> _children) {
15+
val = _val;
16+
children = _children;
17+
}
18+
};
19+
*/
20+
21+
class Solution {
22+
public int maxDepth(Node root) {
23+
if (root == null) {
24+
return 0;
25+
}
26+
else if (root.children.isEmpty()) {
27+
return 1;
28+
}
29+
else {
30+
List<Integer> depths = new LinkedList<>();
31+
for (Node item : root.children) {
32+
depths.add(maxDepth(item));
33+
}
34+
return Collections.max(depths) + 1;
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)