[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 ...
随机推荐
- ERA-Interim 的变量TCW和VIWV可降水量
可降水量(Precipitable water) 气象上有一个名词"可降水量"(Precipitable water),可以用来衡量大气的水含量. 其公式为 \(W=\frac{1 ...
- .net中微信、支付宝回调
需求:自助机调用接口生成二维码,用户扫描二维码付款后,回调方法里写入到数据库,自助机轮询查数据库判断用户是否付款. 1 using bk.Services.Log; 2 using bk.web.Co ...
- 如何搭建Redis集群(主从+哨兵)
一.什么是redis主从复制? 主从复制,是指将一台Redis服务器的数据,复制到其他的Redis服务器.前者称为主节点(master),后者称为从节点(slave),数据的复制是单向的,只能由主节点 ...
- 混淆css类名
使用vite:
- windows中的换行符和Linux中的换行符
# cat -A tmp.tmp 120.4987 12.717858^M$ ^M 对应的字符是 \r # cat tmp.txt | awk -vRS='\r\n' '{print $2,$1}' ...
- 86、linux离线安装nginx
参考 nginx 离线安装https://blog.csdn.net/ywd1992/article/details/83095855
- ORACLE查看表占用空间的大小
查询object的大小,按照降序排序 select * from user_segments s where s.BYTES is not null order by s.BYTES desc ...
- 利用python中的win32com模块操作Word、Excel文件
word操作 doc文件转换为docx文件 安装win32com模块:pip3 install pypiwin32 import os from win32com.client import Disp ...
- 每日一抄 Go语言通信顺序进程简述
package main import ( "fmt" "sync" ) /* Go实现了两种并发形式,第一种是大家普遍认知的多线程共享内存,其实就是 Java ...
- Spring框架常用依赖配置--供使用时直接复制
Spring框架常用依赖配置--供使用时直接复制 以下仅为本人工作.学习过程中所接触到的内容,不足之处请多包涵. <properties> <org.springframework. ...