Skip to content

Commit 9b84a2f

Browse files
add 599
1 parent 11e1c5e commit 9b84a2f

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ Your ideas/fixes/algorithms are more than welcome!
223223
|562|[Longest Line of Consecutive One in Matrix](https://leetcode.com/problems/longest-line-of-consecutive-one-in-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_562.java) | O(m*n) |O(m*n) | |Medium | Matrix DP
224224
|561|[Array Partition I](https://leetcode.com/problems/array-partition-i/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_561.java) | O(nlogn) |O(1) | |Easy | Array
225225
|560|[Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_560.java) | O(n) |O(n) || Medium | Array, HashMap
226+
|559|[Maximum Depth of N-ary Tree](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_559.java) | O(n) |O(n) | |Easy | DFS, recursion
226227
|557|[Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_557.java) | O(n) |O(n) | |Easy | String
227228
|556|[Next Greater Element III](https://leetcode.com/problems/parents-greater-element-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/NextGreaterElementIII.java) | O(n)|O(1)| |Medium | String
228229
|555|[Split Concatenated Strings](https://leetcode.com/problems/split-concatenated-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_555.java) | O(n^2) |O(n) | |Medium | String
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.Node;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* 559. Maximum Depth of N-ary Tree
9+
*
10+
* Given a n-ary tree, find its maximum depth.
11+
*
12+
* The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
13+
*
14+
* For example, given a 3-ary tree:
15+
* 1
16+
* / | \
17+
* 3 2 4
18+
* / \
19+
* 5 6
20+
*
21+
* We should return its max depth, which is 3.
22+
*
23+
* Note:
24+
*
25+
* The depth of the tree is at most 1000.
26+
* The total number of nodes is at most 5000.
27+
*/
28+
public class _559 {
29+
public static class Solution1 {
30+
public int maxDepth(Node root) {
31+
int maxDepth = 0;
32+
if (root == null) {
33+
return maxDepth;
34+
}
35+
List<List<Integer>> allPaths = new ArrayList<>();
36+
List<Integer> currentPath = new ArrayList<>();
37+
dfs(root, currentPath, allPaths);
38+
for (List<Integer> path : allPaths) {
39+
maxDepth = Math.max(path.size(), maxDepth);
40+
}
41+
return maxDepth;
42+
}
43+
44+
private void dfs(Node root, List<Integer> currentPath, List<List<Integer>> allPaths) {
45+
if (root == null) {
46+
allPaths.add(new ArrayList<>(currentPath));
47+
}
48+
if (root.children != null && !root.children.isEmpty()) {
49+
currentPath.add(root.val);
50+
for (Node child : root.children) {
51+
dfs(child, new ArrayList<>(currentPath), allPaths);
52+
}
53+
}
54+
if (root.children == null || root.children.isEmpty()) {
55+
currentPath.add(root.val);
56+
allPaths.add(new ArrayList<>(currentPath));
57+
}
58+
}
59+
}
60+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.Node;
4+
import com.fishercoder.solutions._559;
5+
import java.util.Arrays;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _559Test {
12+
private static _559.Solution1 solution1;
13+
private static Node root;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _559.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
root = new Node(1);
23+
Node node3 = new Node(3);
24+
Node node2 = new Node(2);
25+
Node node4 = new Node(4);
26+
root.children = Arrays.asList(node3, node2, node4);
27+
Node node5 = new Node(5);
28+
Node node6 = new Node(6);
29+
node3.children = Arrays.asList(node5, node6);
30+
31+
assertEquals(3, solution1.maxDepth(root));
32+
}
33+
}

0 commit comments

Comments
 (0)