当前位置:网站首页>牛客-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;
}
}
边栏推荐
- Get the QR code of wechat applet with parameters - and share the source code of modifying the QR code logo
- PostgreSQL DBA quick start - source compilation and installation
- 学习使用php实现无限极评论和无限极转二级评论解决方案
- uniapp 开发微信公众号,下拉框默认选中列表第一个
- How does home office manage the data center network infrastructure?
- 小程序 rich-text中图片点击放大与自适应大小问题
- Floating point notation (summarized from cs61c and CMU CSAPP)
- 简单的价格表样式代码
- numpy.logical_or
- Internet of things? Come and see Arduino on the cloud
猜你喜欢
随机推荐
Graffiti smart brings a variety of heavy smart lighting solutions to the 2022 American International Lighting Exhibition
leetCode-1051: 高度检查器
线程的六种状态
5.菜品管理业务开发
Record the range of data that MySQL update will lock
Troubleshooting steps for Oracle pool connection request timeout
线程调度的常用方法
leetCode-223: 矩形面积
Juul, the American e-cigarette giant, suffered a disaster, and all products were forced off the shelves
SQL Sever中的窗口函数row_number()rank()dense_rank()
How does home office manage the data center network infrastructure?
Safety and food security for teachers and students of the trapped Yingxi middle school
二叉樹第一部分
Operator details
Queue queue
leetCode-498: 對角線遍曆
canvas无限扫描js特效代码
6.套餐管理业务开发
Go language development environment setup +goland configuration under the latest Windows
Nvisual digital infrastructure operation management software platform









