Leetcode PHP题解--D44 590. N-ary Tree Postorder Traversal


590. N-ary Tree Postorder Traversal

题目链接

590. N-ary Tree Postorder Traversal

题目分析

后序遍历,这题也是比较基础的题目了。

思路

先遍历子节点,再遍历根节点。

最终代码

<?php
/*
// Definition for a Node.
class Node {
    public $val;
    public $children;

    @param Integer $val 
    @param list<Node> $children 
    function __construct($val, $children) {
        $this->val = $val;
        $this->children = $children;
    }
}
*/
class Solution {
    public $val = [];
    /**
     * @param Node $root
     * @return Integer[]
     */
    function postorder($root) {
        if(!$root){
            return $this->val;
        }
        foreach($root->children as $child){
            $this->postorder($child);
        }
        $this->val[] = $root->val;
        return $this->val;
    }
}

若觉得本文章对你有用,欢迎用爱发电资助。


点赞 取消点赞 收藏 取消收藏

<< 上一篇: Leetcode PHP题解--D43 589. N-ary Tree Preorder Traversal

>> 下一篇: Hyperledger Fabric PHP SDK