Skip to content

Commit 1af67e8

Browse files
committed
Add solution #589
1 parent dd5a3ca commit 1af67e8

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@
295295
566|[Reshape the Matrix](./0566-reshape-the-matrix.js)|Easy|
296296
567|[Permutation in String](./0567-permutation-in-string.js)|Medium|
297297
575|[Distribute Candies](./0575-distribute-candies.js)|Easy|
298+
589|[N-ary Tree Preorder Traversal](./0589-n-ary-tree-preorder-traversal.js)|Easy|
298299
594|[Longest Harmonious Subsequence](./0594-longest-harmonious-subsequence.js)|Easy|
299300
599|[Minimum Index Sum of Two Lists](./0599-minimum-index-sum-of-two-lists.js)|Easy|
300301
605|[Can Place Flowers](./0605-can-place-flowers.js)|Easy|
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
};

0 commit comments

Comments
 (0)