File tree 2 files changed +42
-0
lines changed
2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 340
340
423|[ Reconstruct Original Digits from English] ( ./0423-reconstruct-original-digits-from-english.js ) |Medium|
341
341
424|[ Longest Repeating Character Replacement] ( ./0424-longest-repeating-character-replacement.js ) |Medium|
342
342
427|[ Construct Quad Tree] ( ./0427-construct-quad-tree.js ) |Medium|
343
+ 429|[ N-ary Tree Level Order Traversal] ( ./0429-n-ary-tree-level-order-traversal.js ) |Medium|
343
344
434|[ Number of Segments in a String] ( ./0434-number-of-segments-in-a-string.js ) |Easy|
344
345
435|[ Non-overlapping Intervals] ( ./0435-non-overlapping-intervals.js ) |Medium|
345
346
437|[ Path Sum III] ( ./0437-path-sum-iii.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 429. N-ary Tree Level Order Traversal
3
+ * https://leetcode.com/problems/n-ary-tree-level-order-traversal/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an n-ary tree, return the level order traversal of its nodes' values.
7
+ *
8
+ * Nary-Tree input serialization is represented in their level order traversal, each group of
9
+ * children is separated by the null value.
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 levelOrder = function ( root ) {
25
+ if ( ! root ) return [ ] ;
26
+
27
+ const result = [ ] ;
28
+ const queue = [ root ] ;
29
+
30
+ while ( queue . length ) {
31
+ const [ level , current ] = [ queue . length , [ ] ] ;
32
+ for ( let i = 0 ; i < level ; i ++ ) {
33
+ const node = queue . shift ( ) ;
34
+ current . push ( node . val ) ;
35
+ queue . push ( ...node . children ) ;
36
+ }
37
+ result . push ( current ) ;
38
+ }
39
+
40
+ return result ;
41
+ } ;
You can’t perform that action at this time.
0 commit comments