File tree 2 files changed +27
-0
lines changed
2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 295
295
566|[ Reshape the Matrix] ( ./0566-reshape-the-matrix.js ) |Easy|
296
296
567|[ Permutation in String] ( ./0567-permutation-in-string.js ) |Medium|
297
297
575|[ Distribute Candies] ( ./0575-distribute-candies.js ) |Easy|
298
+ 589|[ N-ary Tree Preorder Traversal] ( ./0589-n-ary-tree-preorder-traversal.js ) |Easy|
298
299
594|[ Longest Harmonious Subsequence] ( ./0594-longest-harmonious-subsequence.js ) |Easy|
299
300
599|[ Minimum Index Sum of Two Lists] ( ./0599-minimum-index-sum-of-two-lists.js ) |Easy|
300
301
605|[ Can Place Flowers] ( ./0605-can-place-flowers.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 589. N-ary Tree Preorder Traversal
3
+ * https://leetcode.com/problems/n-ary-tree-preorder-traversal/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given the root of an n-ary tree, return the preorder traversal of its nodes' values.
7
+ *
8
+ * Nary-Tree input serialization is represented in their level order traversal. Each group
9
+ * of children is separated by the null value (See examples)
10
+ */
11
+
12
+ /**
13
+ * // Definition for a _Node.
14
+ * function _Node(val, children) {
15
+ * this.val = val;
16
+ * this.children = children;
17
+ * };
18
+ */
19
+
20
+ /**
21
+ * @param {_Node|null } root
22
+ * @return {number[] }
23
+ */
24
+ var preorder = function ( root ) {
25
+ return ! root ? [ ] : [ root . val , ...root . children . flatMap ( n => preorder ( n ) ) ] ;
26
+ } ;
You can’t perform that action at this time.
0 commit comments