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)相关推荐

  1. 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 ...

  2. 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...

  3. 【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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 【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 ...

  8. leetcode python3 简单题104. Maximum Depth of Binary Tree

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零四题 (1)题目 英文: Given a binary tree, find ...

  9. [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 ...

最新文章

  1. 好久没更新日志了啊~!!今天发一个AS3的播放器
  2. 强化学习在出行行业的应用等
  3. java jxl map,jxl操作Excel
  4. jdbc 自增id 原理_面试被问分布式ID怎么办? 滴滴(Tinyid)甩给他
  5. 树形图,多层级目录等其他名称待补充……
  6. PLSQL中文显示乱码
  7. c店店铺名怎么用旗舰店_大发现!三利和洁丽雅2大国民毛巾品牌也有1688店铺!...
  8. 谭老师c语言入门 小甲鱼,《零基础入门学习C语言》视频教程(小甲鱼主讲全套共65节)...
  9. Acer e5-571g 完美黑苹果10.13
  10. ios沙箱软件_ios沙盒2存档-ios沙盒2最新版下载0.5.2苹果版-西西软件下载
  11. Linux下编写udp群聊室
  12. UML核心元素--参与者
  13. MOS管开启过程中VGS的台阶——米勒平台?
  14. k8s健康检查(七)
  15. matlab和opencv混编(mex问题,mexopencv问题)(水下相机折射补偿:Pinax-model)
  16. 计算机键盘上每个键的作用
  17. 深圳赏给我的耳光:说到底,生活就是一场接着一场的较量
  18. 拼多多第二季度表现亮眼,“贪便宜”的你贡献了多少?
  19. isset与empty的区别
  20. c语言家庭生活计划管理系统,[参赛作品]家庭智能鱼缸

热门文章

  1. UE4 ListView
  2. 磁带库加入新磁带(backup exec)
  3. java实现粘贴图片上传
  4. 如何把电脑文件备份?本指南很详细
  5. 雷哥,这个你有点想当然了吧
  6. flex 布局的浏览器兼容性方案
  7. Tableau 超市经典案例之销售分析(三)
  8. Python决策树、随机森林、朴素贝叶斯、KNN(K-最近邻居)分类分析银行拉新活动挖掘潜在贷款客户
  9. 全球与中国儿童服饰市场现状及未来发展趋势(2022)
  10. 3718. 批改作业