资讯专栏INFORMATION COLUMN

Leetcode PHP题解--D41 104. Maximum Depth of Binary T

LMou / 3149人阅读

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

104. Maximum Depth of Binary Tree 题目链接

104. Maximum Depth of Binary Tree

题目分析

返回给定的二叉树有多少层。

思路

每下一级,层树+1,并记录到类属性level中。并判断是否大于已知最深层树。

最终代码
val = $value; }
 * }
 */
class Solution {
    public $max = 0;
    public $level = 0;
    /**
     * @param TreeNode $root
     * @return Integer
     */
    function maxDepth($root) {
        if($root){
            $this->level++;
        }
        if($this->level>=$this->max){
            $this->max = $this->level;
        }
        if($root->left){
            $this->maxDepth($root->left);
        }
        if($root->right){
            $this->maxDepth($root->right);
        }
        $this->level--;
        return $this->max;
    }
}

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

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

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

相关文章

  • LeetCode 攻略 - 2019 年 7 月上半月汇总(55 题攻略)

    摘要:微信公众号记录截图记录截图目前关于这块算法与数据结构的安排前。已攻略返回目录目前已攻略篇文章。会根据题解以及留言内容,进行补充,并添加上提供题解的小伙伴的昵称和地址。本许可协议授权之外的使用权限可以从处获得。 Create by jsliang on 2019-07-15 11:54:45 Recently revised in 2019-07-15 15:25:25 一 目录 不...

    warmcheng 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月汇总(100 题攻略)

    摘要:月下半旬攻略道题,目前已攻略题。目前简单难度攻略已经到题,所以后面会调整自己,在刷算法与数据结构的同时,攻略中等难度的题目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道题,目前已攻略 100 题。 一 目录 不折腾的前端,和咸鱼有什么区别...

    tain335 评论0 收藏0
  • Leetcode PHP题解--D42 559. Maximum Depth of N-ary Tr

    摘要:题目链接题目分析此题和上一题思路一样。只是不是二叉树。思路略最终代码若觉得本文章对你有用,欢迎用爱发电资助。 D42 559. Maximum Depth of N-ary Tree 题目链接 559. Maximum Depth of N-ary Tree 题目分析 此题和上一题思路一样。只是不是二叉树。而是正常的树。 思路 略 最终代码

    CrazyCodes 评论0 收藏0
  • LeetCode 104 Maximum Depth of Binary Tree 二叉树最大深度

    LeetCode 104 Maximum Depth of Binary Tree难度:Easy 题目描述:找到一颗二叉树的最深深度。Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down ...

    PiscesYE 评论0 收藏0
  • LeetCode 之 JavaScript 解答第104题 —— 二叉树的最大深度

    摘要:小鹿题目二叉树的最大深度给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。求二叉树的深度,必然要用到递归来解决。分别递归左右子树。 Time:2019/4/22Title: Maximum Depth of Binary TreeDifficulty: MediumAuthor:小鹿 题目:Maximum Depth of Binary Tre...

    boredream 评论0 收藏0

发表评论

0条评论

LMou

|高级讲师

TA的文章

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