- 栈
/**
* // Definition for a Node.
* function Node(val, children) {
* this.val = val;
* this.children = children;
* };
*/
/**
* @param {Node} root
* @return {number[]}
*/
var preorder = function (root) {
const results = [];
if (!root) {
return results;
}
const stack = [root];
while (stack.length) {
const node = stack.pop();
results.push(node.val);
while (node.children && node.children.length) {
stack.push(node.children.pop());
}
}
return results;
};