资讯专栏INFORMATION COLUMN

LeetCode 104 Maximum Depth of Binary Tree 二叉树最大深度

PiscesYE / 2154人阅读

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 to the farthest leaf node.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

    3
   / 
  9  20
    /  
   15   7

return its depth = 3.

解题思路:
Solution 1:
解二叉树的题目一般如果难度为easy,则要求iterative和recursive都会写,二叉树的最深深度是左子树和右子树的Max深度,根据这一特性,我们自底向上,时间复杂度O(n), Space O(1), 但因为是递归,所以会占用stack,recursive的写法如下:

public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        //get the left and right depth
        int leftDep = maxDepth(root.left);
        int rightDep = maxDepth(root.right);
        return 1 + Math.max(leftDep, rightDep);
    }

解题思路:
Solution 2:
使用BFS方法iterative使用一个Queue,每往下一层都加1,最后拿到最深深度。
时间:O(n), 空间 O(n)

    public int maxDepth(TreeNode root) {
        //iterative
        if (root == null) return 0;
        Queue q = new LinkedList<>();
        q.offer(root);
        int dep = 0;
        while(!q.isEmpty()) {
            dep++;
            int size = q.size();
            for(int i = 0; i < size; i++) {
                TreeNode now = q.poll();
                if (now.left != null) q.offer(now.left);
                if (now.right != null) q.offer(now.right);
            }

        }
        return dep;
        
    }

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

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

相关文章

  • LeetCode 之 JavaScript 解答第104题 —— 叉树最大深度

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

    boredream 评论0 收藏0
  • 前端 | 每天一个 LeetCode

    摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...

    张汉庆 评论0 收藏0
  • 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题解--D41 104. Maximum Depth of Binary T

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

    LMou 评论0 收藏0

发表评论

0条评论

PiscesYE

|高级讲师

TA的文章

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