2020-07-26 589. N-ary Tree Preor

2020-07-26  本文已影响0人  苦庭

https://leetcode.com/problems/n-ary-tree-preorder-traversal/

/**
 * // Definition for a Node.
 * function Node(val, children) {
 *    this.val = val;
 *    this.children = children;
 * };
 */

/**
 * @param {Node} root
 * @return {number[]}
 */
var preorder = function(root) {
    let res=[], stack = [root];
    while(stack.length) {
        let cur = stack.shift();
        if(!cur) continue;
        res.push(cur.val);
        stack.unshift(...cur.children);
    }
    return res;
};
上一篇 下一篇

猜你喜欢

热点阅读