首页 > 其他 > 详细

590. N叉树的后序遍历

时间:2020-05-22 11:14:43      阅读:52      评论:0      收藏:0      [点我收藏+]

地址:https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/

/**
 * Definition for a Node.
 * class Node {
 *     public $val = null;
 *     public $children = null;
 *     function __construct($val = 0) {
 *         $this->val = $val;
 *         $this->children = array();
 *     }
 * }
 */

class Solution {
    /**
     * @param Node $root
     * @return integer[]
     */
    function postorder($root) {
        $res = [];
        $this->helper($root,$res);
        $res[]=$root->val;
        return $res;
    }

    function helper($root,&$res){
        if($root == null) return ;
        foreach($root->children as $children){
            $this->helper($children,$res);
            $res[]= $children->val;
        }
    }
}

 

590. N叉树的后序遍历

原文:https://www.cnblogs.com/8013-cmf/p/12935950.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!