Leetcode PHP题解--D42 559. Maximum Depth of N-ary Tree


559. Maximum Depth of N-ary Tree

题目链接

559. Maximum Depth of N-ary Tree

题目分析

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

思路

最终代码

<?php
class 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;
    }
}

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


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

<< 上一篇: Leetcode基础刷题之PHP解析(342,344,349)

>> 下一篇: Leetcode基础刷题之PHP解析(350,371)