Leetcode 589 - N-ary tree preorder traversal

Note:

  • Recursive way is too easy, let’s user iterative.
  • Use stack.
  • Add children backwards using pop().

Question:

Given the root of an n-ary tree, return the preorder traversal of its nodes’ values.

Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)

Example:

img

1
2
Input: root = [1,null,3,2,4,null,5,6]
Output: [1,3,5,6,2,4]

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* // Definition for a Node.
* function Node(val, children) {
* this.val = val;
* this.children = children;
* };
*/

/**
* @param {Node|null} root
* @return {number[]}
*/
var preorder = function(root) {
if (!root) return [];
let stack = [root];
let res = [];
while (stack.length > 0) {
const top = stack.pop();
res.push(top.val);
const children = top.children;
while (children.length > 0) {
stack.push(children.pop());
}
}
return res;
};