590. N叉树的后序遍历

地址: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;
        }
    }
}

 

posted @ 2020-05-22 11:04  花花妹子。  阅读(127)  评论(0编辑  收藏  举报