当前位置:网站首页>牛客-TOP101-BM28
牛客-TOP101-BM28
2022-06-24 09:46:00 【一条吃猫的鱼】
核心思路:层序遍历+Deque
import java.util.*;
/* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */
public class Solution {
/** * * @param root TreeNode类 * @return int整型 */
public int maxDepth (TreeNode root) {
// write code here
if(root == null){
return 0;
}
Deque<TreeNode> deque = new ArrayDeque<>();
deque.addLast(root);
int count = 0;
while(!deque.isEmpty()){
int n = deque.size();
for(int i = 0; i < n; i++){
TreeNode node = deque.pollFirst();
if(node.left != null)
deque.offerLast(node.left);
if(node.right != null)
deque.offerLast(node.right);
}
count++;
}
return count;
}
}
public class Solution {
/** * * @param root TreeNode类 * @return int整型 */
public int maxDepth (TreeNode root) {
if(root == null){
return 0;
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
边栏推荐
- 使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?
- 保健品一物一码防窜货营销软件开发
- uniapp开发微信小程序,显示地图功能,且点击后打开高德或腾讯地图。
- How to standardize data center infrastructure management process
- Troubleshooting steps for Oracle pool connection request timeout
- Development of anti fleeing marketing software for health products
- 4. classification management business development
- Why is JSX syntax so popular?
- 如何在一个页面上使用多个KindEditor编辑器并将值传递到服务器端
- tf.errors
猜你喜欢

Nvisual digital infrastructure operation management software platform

Geogebra instance clock

canvas掉落的小球重力js特效动画

保健品一物一码防窜货营销软件开发

Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5

Internet of things? Come and see Arduino on the cloud

使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?

1.项目环境搭建

小程序学习之获取用户信息(getUserProfile and getUserInfo)

二叉樹第一部分
随机推荐
Leetcode interview question 01.05: primary editing
时尚的弹出模态登录注册窗口
3.员工的增删改查
解决微信小程序rich-text富文本标签内部图片宽高自适应的方法
Internet of things? Come and see Arduino on the cloud
Leetcode-498: diagonal traversal
Binary tree part I
Indexeddb local storage, homepage optimization
Network of test and development - Common Service Protocols
Graffiti smart brings a variety of heavy smart lighting solutions to the 2022 American International Lighting Exhibition
[db2] sql0805n solution and thinking
线程的六种状态
利用pandas读取SQL Sever数据表
canvas管道动画js特效
学习使用KindEditor富文本编辑器,点击上传图片遮罩太大或白屏解决方案
CVPR 2022 Oral | 英伟达提出自适应token的高效视觉Transformer网络A-ViT,不重要的token可以提前停止计算
Wechat applet learning to achieve list rendering and conditional rendering
Desktop software development framework reward
机器学习——感知机及K近邻
Observer mode