Leetcode-PHP题解D42-559-Maximum-Depth-of-Nary-Tree

8次阅读

共计 393 个字符,预计需要花费 1 分钟才能阅读完成。

D42 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;
    }
}

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

正文完
 0