当前位置:网站首页>Niuke-top101-bm28
Niuke-top101-bm28
2022-06-24 10:29:00 【A fish that eats cats】
The core idea : Sequence traversal +Deque
import java.util.*;
/* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */
public class Solution {
/** * * @param root TreeNode class * @return int integer */
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 class * @return int integer */
public int maxDepth (TreeNode root) {
if(root == null){
return 0;
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
边栏推荐
- Caching mechanism for wrapper types
- Six states of threads
- What you must know about distributed systems -cap
- np.float32()
- International Symposium on energy and environmental engineering in 2022 (coeee 2022)
- 2022 the most complete and detailed JMeter interface test tutorial and detailed interface test process in the whole network - JMeter test plan component (thread < user >)
- Younger sister Juan takes you to learn JDBC --- 2-day sprint Day1
- 包装类型的缓存机制
- uniapp 开发微信公众号,下拉框默认选中列表第一个
- [resource sharing] the 5th International Conference on civil, architectural and environmental engineering in 2022 (iccaee 2022)
猜你喜欢

自定义kindeditor编辑器的工具栏,items即去除不必要的工具栏或者保留部分工具栏

解决Deprecated: Methods with the same name as their class will not be constructors in报错方案

H5网页如何在微信中自定义分享链接

Status of the thread pool

charles抓包工具使用教程

线程的六种状态

HBuilder制作英雄皮肤抽奖小游戏

Machine learning - principal component analysis (PCA)

SQL Sever关于like操作符(包括字段数据自动填充空格问题)

希尔排序图文详解+代码实现
随机推荐
静态链接库和动态链接库的区别
leetCode-2221: 数组的三角和
[ei sharing] the 6th International Conference on ship, ocean and Maritime Engineering in 2022 (naome 2022)
包装类型与基本类型的区别
Flink集群搭建以及企业级yarn集群搭建
numpy.logical_or
Leetcode-1823: find the winner of the game
【资源分享】2022年第五届土木,建筑与环境工程国际会议(ICCAEE 2022)
分布式事务原理以及解决分布式事务方案
Sort out interface performance optimization skills and kill slow code
使用swiper左右轮播切换时,Swiper Animate的动画失效,怎么解决?
SQL Sever关于like操作符(包括字段数据自动填充空格问题)
[resource sharing] the 5th International Conference on civil, architectural and environmental engineering in 2022 (iccaee 2022)
消息队列的作用
进程与多线程
SSM整合
Wechat applet rich text picture width height adaptive method introduction (rich text)
线程调度的常用方法
正规方程、、、
解决微信小程序rich-text富文本标签内部图片宽高自适应的方法