Skip to content

Commit d8e67fd

Browse files
add 589
1 parent 5d61f01 commit d8e67fd

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Your ideas/fixes/algorithms are more than welcome!
191191
|593|[Valid Square](https://leetcode.com/problems/valid-square/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_593.java) | O(1) |O(1) | |Medium | Math
192192
|592|[Fraction Addition and Subtraction](https://leetcode.com/problems/fraction-addition-and-subtraction/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_592.java) | O(nlogx) |O(n) | |Medium | Math
193193
|591|[Tag Validator](https://leetcode.com/problems/tag-validator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_591.java) | O(n) |O(n) | |Hard | Stack, String
194+
|589|[N-ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_589.java) | O(n) |O(n) | |Easy | DFS, recursion
194195
|588|[Design In-Memory File System](https://leetcode.com/problems/design-in-memory-file-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_588.java) | O(n) |O(h) | |Hard | Trie, Design
195196
|587|[Erect the Fence](https://leetcode.com/problems/erect-the-fence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_587.java) | O(?) |O(?) | |Hard | Geometry
196197
|583|[Delete Operation for Two Strings](https://leetcode.com/problems/delete-operation-for-two-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_583.java) | O(m*n) |O(m*n) could be optimized to O(n) | |Medium | DP
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder.common.classes;
2+
3+
import java.util.List;
4+
5+
public class Node {
6+
public int val;
7+
public List<Node> children;
8+
9+
public Node() {
10+
}
11+
12+
public Node(int _val, List<Node> _children) {
13+
val = _val;
14+
children = _children;
15+
}
16+
17+
//todo: implement this method
18+
/**return a N-ary tree based on the preorder values*/
19+
public static Node createNaryTree(List<Integer> preorderValues) {
20+
return null;
21+
}
22+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
* 589. N-ary Tree Preorder Traversal
9+
*
10+
* Given an n-ary tree, return the preorder traversal of its nodes' values.
11+
*
12+
* For example, given a 3-ary tree:
13+
*
14+
* 1
15+
* / | \
16+
* 3 2 4
17+
* / \
18+
* 5 6
19+
*
20+
* Return its preorder traversal as: [1,3,5,6,2,4].
21+
*
22+
* Note:
23+
*
24+
* Recursive solution is trivial, could you do it iteratively?
25+
*/
26+
public class _589 {
27+
public static class Solution1 {
28+
public List<Integer> preorder(Node root) {
29+
List<Integer> result = new ArrayList<>();
30+
if (root == null) {
31+
return result;
32+
}
33+
dfs(root, result);
34+
return result;
35+
}
36+
37+
private void dfs(Node root, List<Integer> result) {
38+
if (root == null) {
39+
return;
40+
}
41+
result.add(root.val);
42+
if (root.children.size() > 0) {
43+
for (Node child : root.children) {
44+
dfs(child, result);
45+
}
46+
}
47+
}
48+
}
49+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.Node;
4+
import com.fishercoder.solutions._589;
5+
import java.util.List;
6+
import org.junit.BeforeClass;
7+
import org.junit.Ignore;
8+
import org.junit.Test;
9+
10+
import static org.junit.Assert.assertArrayEquals;
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class _589Test {
14+
private static _589.Solution1 solution1;
15+
private static Node root;
16+
private static List<Integer> expectedPreOrder;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _589.Solution1();
21+
}
22+
23+
@Test
24+
@Ignore//todo: Node.createNaryTree method hasn't been implemented yet
25+
public void test1() {
26+
expectedPreOrder = List.of(1, 3, 5, 6, 2, 4);
27+
root = Node.createNaryTree(expectedPreOrder);
28+
assertEquals(expectedPreOrder, solution1.preorder(root));
29+
}
30+
}

0 commit comments

Comments
 (0)