Skip to content

Commit 3a67a61

Browse files
solves n ary tree preorder traversal
1 parent fc01d34 commit 3a67a61

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
| 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree) | Easy | [![Java](assets/java.png)](src/SubtreeOfAnotherTree.java) [![Python](assets/python.png)](python/subtree_of_another_tree.py) |
154154
| 575 | [Distribute Candies](https://leetcode.com/problems/distribute-candies) | Easy | [![Java](assets/java.png)](src/DistributeCandies.java) [![Python](assets/python.png)](python/distribute_candies.py) |
155155
| 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray) | Medium | [![Java](assets/java.png)](src/ShortestUnsortedContinuousSubarray.java) [![Python](assets/python.png)](python/shortest_continuous_unsorted_subarray.py) |
156-
| 589 | [N-Ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal) | Easy | |
156+
| 589 | [N-Ary Tree Preorder Traversal](https://leetcode.com/problems/n-ary-tree-preorder-traversal) | Easy | [![Java](assets/java.png)](src/NArayTreePreOrderTraversal.java) [![Python](assets/python.png)](python/n_ary_tree_preorder_traversal.py) |
157157
| 590 | [N-Ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal) | Easy | |
158158
| 594 | [Longest Harmonious Subsequence](https://leetcode.com/problems/longest-harmonious-subsequence) | Easy | |
159159
| 598 | [Range Addition II](https://leetcode.com/problems/range-addition-ii) | Easy | |

Diff for: python/n_ary_tree_preorder_traversal.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a Node.
2+
from typing import List
3+
4+
5+
class Node:
6+
def __init__(self, val=None, children=None):
7+
self.val = val
8+
self.children = children
9+
10+
11+
class Solution:
12+
def __init__(self):
13+
self.result = []
14+
15+
def preorderTraversal(self, root: Node) -> None:
16+
if root is None: return None
17+
self.result.append(root.val)
18+
for child in root.children:
19+
self.preorderTraversal(child)
20+
21+
def preorder(self, root: Node) -> List[int]:
22+
self.preorderTraversal(root)
23+
return self.result

Diff for: src/NArayTreePreOrderTraversal.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class NArayTreePreOrderTraversal {
5+
List<Integer> result = new ArrayList<>();
6+
7+
public List<Integer> preorder(Node root) {
8+
preorderTraversal(root);
9+
return result;
10+
}
11+
12+
private void preorderTraversal(Node root) {
13+
if (root == null) return;
14+
result.add(root.val);
15+
for (Node child :root.children) {
16+
preorderTraversal(child);
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)