104. Maximum Depth of Binary Tree (Easy)
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from
the root node down to the farthest leaf node.
题目即求二叉树的高度,层序遍历二叉树求解即可。
Solution:
Java:
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
public class Solution {public int maxDepth(TreeNode root) {if(root == null) {return 0;}if(root.left == null && root.right == null) {return 1;}int maxDepth = 0;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);int currentLevelCount = 1;int nextLevelCount = 0;while(!queue.isEmpty()) {TreeNode node = queue.poll();currentLevelCount--;if(node.left != null) {queue.add(node.left);nextLevelCount++;}if(node.right != null) {queue.add(node.right);nextLevelCount++;}if(currentLevelCount == 0) {currentLevelCount = nextLevelCount;nextLevelCount = 0;maxDepth++;}}return maxDepth;}
}
非递归的层序遍历,通过队列来实现,使用currentLevelCount和nextLevelCount两个变量记录当前层的结点个数和下一层的结点个数。
104. Maximum Depth of Binary Tree (Easy)相关推荐
- LC 104. Maximum Depth of Binary Tree
1.题意 104. Maximum Depth of Binary Tree Easy 98540 Given a binary tree, find its maximum depth. The m ...
- 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...
- 【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 ...
- Leet Code OJ 104. Maximum Depth of Binary Tree [Difficulty: Easy]
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- LeetCode: 104. Maximum Depth of Binary Tree
题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...
- leetcode 104. 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
1. 题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th ...
- leetcode python3 简单题104. Maximum Depth of Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零四题 (1)题目 英文: Given a binary tree, find ...
- [swift] LeetCode 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
最新文章
- 好久没更新日志了啊~!!今天发一个AS3的播放器
- 强化学习在出行行业的应用等
- java jxl map,jxl操作Excel
- jdbc 自增id 原理_面试被问分布式ID怎么办? 滴滴(Tinyid)甩给他
- 树形图,多层级目录等其他名称待补充……
- PLSQL中文显示乱码
- c店店铺名怎么用旗舰店_大发现!三利和洁丽雅2大国民毛巾品牌也有1688店铺!...
- 谭老师c语言入门 小甲鱼,《零基础入门学习C语言》视频教程(小甲鱼主讲全套共65节)...
- Acer e5-571g 完美黑苹果10.13
- ios沙箱软件_ios沙盒2存档-ios沙盒2最新版下载0.5.2苹果版-西西软件下载
- Linux下编写udp群聊室
- UML核心元素--参与者
- MOS管开启过程中VGS的台阶——米勒平台?
- k8s健康检查(七)
- matlab和opencv混编(mex问题,mexopencv问题)(水下相机折射补偿:Pinax-model)
- 计算机键盘上每个键的作用
- 深圳赏给我的耳光:说到底,生活就是一场接着一场的较量
- 拼多多第二季度表现亮眼,“贪便宜”的你贡献了多少?
- isset与empty的区别
- c语言家庭生活计划管理系统,[参赛作品]家庭智能鱼缸