资讯专栏INFORMATION COLUMN

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

CrazyCodes / 3269人阅读

摘要:题目链接题目分析此题和上一题思路一样。只是不是二叉树。思路略最终代码若觉得本文章对你有用,欢迎用爱发电资助。

D42 559. Maximum Depth of N-ary Tree 题目链接

559. Maximum Depth of N-ary Tree

题目分析

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

思路

最终代码
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;
    }
}

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

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/31316.html

相关文章

  • [LeetCode] 559. Maximum Depth of N-ary Tree

    Problem Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. For example, given a 3-ary tree: ...

    EdwardUp 评论0 收藏0
  • Leetcode PHP题解--D55 429. N-ary Tree Level Order Tr

    摘要:题目链接题目分析按层遍历叉树。思路以层数为键,塞入当前节点的值。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D55 429. N-ary Tree Level Order Traversal 题目链接 429. N-ary Tree Level Order Traversal 题目分析 按层遍历N叉树。 思路 以层数为键,塞入当前节点的值。 递归遍历即可。 最终代码

    libxd 评论0 收藏0
  • Leetcode PHP题解--D41 104. Maximum Depth of Binary T

    摘要:题目链接题目分析返回给定的二叉树有多少层。思路每下一级,层树,并记录到类属性中。并判断是否大于已知最深层树。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 104. Maximum Depth of Binary Tree 题目链接 104. Maximum Depth of Binary Tree 题目分析 返回给定的二叉树有多少层。 思路 每下一级,层树+1,并记录到类属性lev...

    LMou 评论0 收藏0
  • Leetcode PHP题解--D43 589. N-ary Tree Preorder Trave

    摘要:题目链接题目分析维数组的先序遍历。这题也不想多说什么了。是比较基础的题目了。先序就是先根后子而已。思路在遍历子节点之前,先保存当前节点的信息。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D43 589. N-ary Tree Preorder Traversal 题目链接 589. N-ary Tree Preorder Traversal 题目分析 N维数组的先序遍历。 这题也...

    junbaor 评论0 收藏0
  • Leetcode PHP题解--D44 590. N-ary Tree Postorder Trav

    摘要:题目链接题目分析后序遍历,这题也是比较基础的题目了。思路先遍历子节点,再遍历根节点。最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D44 590. N-ary Tree Postorder Traversal 题目链接 590. N-ary Tree Postorder Traversal 题目分析 后序遍历,这题也是比较基础的题目了。 思路 先遍历子节点,再遍历根节点。 最终代码...

    Songlcy 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<