D42 559. Maximum Depth of N-ary Tree

题目链接

559. Maximum Depth of N-ary Tree

题目分析

此题和上一题思路一样。只是不是二叉树。而是正常的树。

思路

最终代码

<?phpclass Solution {    public $max = 0;    public $level = 0;    function maxDepth($root) {        if($root){            $this->level++;            if($this->level>=$this->max){                $this->max = $this->level;            }        }        if($root->children){            foreach($root->children as $child){                $this->maxDepth($child);                }        }        if($root){            $this->level--;        }        return $this->max;    }}

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