题目

求二叉树的深度,即根节点出发的最长路径上点的个数,即最长路径+1(本身这个点

https://leetcode.com/problems/maximum-depth-of-binary-tree/

Given the root of a binary tree, return its maximum depth.

A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: 3

Example 2:

Input: root = [1,null,2]
Output: 2

Example 3:

Input: root = []
Output: 0

Example 4:

Input: root = [0]
Output: 1

思路

DFS思路参考最长直径https://leetcode.com/problems/diameter-of-binary-tree/

这里只用找到一边,不用相加,所以是max(left,right)

代码

class Solution {
public int maxDepth(TreeNode root) {
int ans=dfs(root,0);
return ans;
} public int dfs(TreeNode root,int ans){
if(root==null)
return 0;
int left=dfs(root.left,ans);
int right=dfs(root.right,ans);
ans+=Math.max(left,right);
return ans+1;
}
}

[Leetcode 104]二叉树最大深度Maximum Depth of Binary Tree的更多相关文章

  1. LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)

    104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...

  2. [Swift]LeetCode104. 二叉树的最大深度 | Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  3. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

  4. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

  5. [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  7. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

    Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the ...

  8. 104. Maximum Depth of Binary Tree(C++)

    104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...

  9. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  10. 【LeetCode练习题】Maximum Depth of Binary Tree

    Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...

随机推荐

  1. 获取git远程分支仓库

    1:新建本地目录 2:进入并初始化这个目录 git init 3:新建一个文件,并添加 git add . 4: git commit -m "first commit" 5:新建 ...

  2. ICPC2020 World Final

    ICPC2020 WF C 洛谷 题意:给定矩形区域左下角\((0,0)\),右上角\((dx,dy)\),其中\(2<=dx,dy<=10^5\),在矩形区域有\(n(n<=100 ...

  3. Redis 突然变慢了如何排查并解决?

    业务场景 某购物平台打算举行"双十一"大型购物狂欢活动,到了半夜12点用户数量暴增,出现了一个技术故障,用户发现自己无法下单!!! 技术组立即组织人手进行故障排查,结论是 Redi ...

  4. centos6.x配置虚拟主机名及域名hosts

    我们在本地安装了centos或者虚拟主机上安装了centos,主机名称默认是localhost,这样我们可以使用localhost访问我们的主机,在终端命令里默认的是[root@localhost ~ ...

  5. 1.Easy Touch 3.1

    Easy Touch 3.1 Hedgehog Team(导入 Easy Touch 插件时自动在菜单栏) Extensions: 拓展 Adding a new joytick: 虚拟摇杆 Addi ...

  6. ORACLE查看会话的大小及终止会话

    一.出现PGA不足时,我们可以查看用户会话大小,结束相应会话 方法一 Select Server, Osuser, Name, Value / 1024 / 1024 Mb, s.Sql_Id, Sp ...

  7. BurpSuite暴力破解和防御实战

    burpsuite暴力破解 工具准备 burp suite 用于攻击web 应用程序的集成平台 jsEncrypter 一个用于前端加密Fuzz的Burp Suite插件,支持base64.sha.m ...

  8. git当前分支修改文件不提交切换另一个分支(git stash)

    出发前先讲故事:项目正在test分支更新版本我们疯狂的写bug,突然没更新前的版本出bug了(意料之中),此时呢我们要切换到master分支去改bug.此时此刻我test分支上修改的文件还不想提交,但 ...

  9. CSOL大灾变移植记录

    在2019年,我玩u3d把玩了一段时间,并制作了一些Demo,其中包括FPSDemo,RPG动作游戏Demo,一些截图如下: 时间到了2020年5月底,我开始玩之前大学研究过的jMonkeyEngin ...

  10. 微信小程序注册、登录小功能都在这

    微信小程序实现注册.登录页面的小功能整理,希望对大家有帮助. 1. 正则验证手机号码 var mobile = that.data.phone;     var myreg = /^(((13[0-9 ...