zoukankan      html  css  js  c++  java
  • LeetCode 590 N 叉树的后序遍历

    给定一个 N 叉树,返回其节点值的后序遍历。

    例如,给定一个 3叉树 :

    返回其后序遍历: [5,6,3,2,4,1].

    说明: 递归法很简单,你可以使用迭代法完成此题吗?

    1. 递归

    /**
     * // Definition for a Node.
     * function Node(val,children) {
     *    this.val = val;
     *    this.children = children;
     * };
     */
    
    /**
     * @param {Node} root
     * @return {number[]}
     */
    var postorder = function (root) {
        let nums = [];
        let func = (root) => {
            root.children && root.children.forEach((item) => func(item));
            nums.push(root.val);
        }
        root && func(root);
        return nums;
    };
    
  • 相关阅读:
    二人组
    对于软件工程的理解
    shell 远程链接
    shell变量
    shell教程
    正则表达式--练习
    git--版本库
    git-版本回退
    git--时光穿梭
    git安装
  • 原文地址:https://www.cnblogs.com/ssaylo/p/13469079.html
Copyright © 2011-2022 走看看