美文网首页
2020-07-26 589. N-ary Tree Preor

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

作者: 苦庭 | 来源:发表于2020-07-26 06:27 被阅读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;
};

相关文章

网友评论

      本文标题:2020-07-26 589. N-ary Tree Preor

      本文链接:https://www.haomeiwen.com/subject/gkwilktx.html