File tree 3 files changed +43
-1
lines changed
3 files changed +43
-1
lines changed Original file line number Diff line number Diff line change 153
153
| 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 ) |
154
154
| 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 ) |
155
155
| 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 ) |
157
157
| 590 | [ N-Ary Tree Postorder Traversal] ( https://leetcode.com/problems/n-ary-tree-postorder-traversal ) | Easy | |
158
158
| 594 | [ Longest Harmonious Subsequence] ( https://leetcode.com/problems/longest-harmonious-subsequence ) | Easy | |
159
159
| 598 | [ Range Addition II] ( https://leetcode.com/problems/range-addition-ii ) | Easy | |
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments