[Leetcode 104]二叉树最大深度Maximum Depth of Binary Tree
题目
求二叉树的深度,即根节点出发的最长路径上点的个数,即最长路径+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的更多相关文章
- LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...
- [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 ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- 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 ...
- [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 ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- 【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 ...
- 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 ...
- 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 ...
- 【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 ...
随机推荐
- ssh远程连接服务器
远程连接服务器及压缩文件与解压 1 终端连接 ssh -p 端口号 用户名@地址 例如:ssh -p 80 name@host 每次远程连接,使用账号和密码登录,有可能使得端口被监听到,从而导致挖矿情 ...
- 代码行数统计(指定目录下所有文件的Line)
遍历文件夹计算文件行数(Windows) 主要使用的是 FindFirstFile. FindNextFile函数寻找子目录下的文件,使用 WIN32_FIND_DATA(文件属性) 结构体 #def ...
- Java-面向对象基础 this& 重载
1.this表示当前对象 获取当前对象的属性 使用this调用当前属性 2.重载 如果两个方法的方法名相同,但参数不一致,那么可以说一个方法是另一个方法的重载
- 剪裁圆形图片cropie
<!DOCTYPE html> <html> <head> <script src="https://cdn.bootcss.com/jquery/ ...
- Word02 领慧讲堂就业讲座office真题
1.课程的讲解之前,先来对题目进行分析,首先需要在考生文件夹下,将Wrod素材.docx文件另存为Word.docx,后续操作均基于此文件,否则不得分. 2.这一步非常的简单,打开下载素材文件,在[文 ...
- Expected indentation of 2 spaces but found 4
预期缩进2个空格,但发现4个 把缩进空格修改后如图
- .NET Core 3.0 WebApi 使用Swagger
1.安装指定版本: Swashbuckle.AspNetCore 5.0.0-rc4(目前稳定版本4.0.1在AspNetCore3.0中会报错误) 2.后台C#代码要严格格式必须加[HttpPost ...
- sql 加工后--小文件解决方案
10.24.8.5 # 切换用户 su - hive # 查看表文件 [hive@hadoop-0001 ~]$ hdfs dfs -ls /user/hive/warehouse/bibase.db ...
- Linux下找不到SO的解决方法
Linux下找不到so文件的解决办法 1)将.so文件路径的目录添加到/etc/ld.so.conf sudo vim /etc/ld.so.conf 将你的SO文件存放路径的根目录写进去(不带so本 ...
- holiday08
第八天 管道 linux允许将 一个命令的输出 可以 通过管道 作为 另一个命令的输入 ls -lh | more 可以理解现实生活中的管子,管子一头塞东西进去,另一头取出来,这里 | 的左右分为两端 ...