Skip to content

Latest commit

 

History

History
36 lines (30 loc) · 677 Bytes

589. N叉树的前序遍历.md

File metadata and controls

36 lines (30 loc) · 677 Bytes
/**
 * // 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;
};