589. N叉树的前序遍历

地址:https://leetcode-cn.com/problems/n-ary-tree-preorder-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 preorder($root) {
        $res = [];
        $this->helper($root,$res);
        return $res;
    }

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

  

原文地址:https://www.cnblogs.com/8013-cmf/p/12936029.html