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.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,null,15,7],

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

return its depth = 3.

求二叉树的最大深度问题用到深度优先搜索 Depth First Search,递归的完美应用,跟求二叉树的最小深度问题原理相同,参见代码如下:

C++ 解法一:

  1. class Solution {
  2. public:
  3. int maxDepth(TreeNode* root) {
  4. if (!root) return ;
  5. return + max(maxDepth(root->left), maxDepth(root->right));
  6. }
  7. };

Java 解法一:

  1. public class Solution {
  2. public int maxDepth(TreeNode root) {
  3. return root == null ? 0 : (1 + Math.max(maxDepth(root.left), maxDepth(root.right)));
  4. }
  5. }

我们也可以使用层序遍历二叉树,然后计数总层数,即为二叉树的最大深度,注意 while 循环中的 for 循环的写法有个 trick,一定要将 q.size() 放在初始化里,而不能放在判断停止的条件中,因为q的大小是随时变化的,所以放停止条件中会出错,参见代码如下:

C++ 解法二:

  1. class Solution {
  2. public:
  3. int maxDepth(TreeNode* root) {
  4. if (!root) return ;
  5. int res = ;
  6. queue<TreeNode*> q{{root}};
  7. while (!q.empty()) {
  8. ++res;
  9. for (int i = q.size(); i > ; --i) {
  10. TreeNode *t = q.front(); q.pop();
  11. if (t->left) q.push(t->left);
  12. if (t->right) q.push(t->right);
  13. }
  14. }
  15. return res;
  16. }
  17. };

Java 解法二:

  1. public class Solution {
  2. public int maxDepth(TreeNode root) {
  3. if (root == null) return 0;
  4. int res = 0;
  5. Queue<TreeNode> q = new LinkedList<>();
  6. q.offer(root);
  7. while (!q.isEmpty()) {
  8. ++res;
  9. for (int i = q.size(); i > 0; --i) {
  10. TreeNode t = q.poll();
  11. if (t.left != null) q.offer(t.left);
  12. if (t.right != null) q.offer(t.right);
  13. }
  14. }
  15. return res;
  16. }
  17. }

Github 同步地址:

https://github.com/grandyang/leetcode/issues/104

类似题目:

Balanced Binary Tree

Minimum Depth of Binary Tree

Maximum Depth of N-ary Tree

参考资料:

https://leetcode.com/problems/maximum-depth-of-binary-tree/

https://leetcode.com/problems/maximum-depth-of-binary-tree/discuss/34207/my-code-of-c-depth-first-search-and-breadth-first-search

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度的更多相关文章

  1. LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

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

  3. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

  4. Leetcode 104 Maximum Depth of Binary Tree 二叉树

    计算二叉树的最大深度 我的方法是找出两个子树的长度中最长的那个,然后加1 class Solution { public: int maxDepth(TreeNode* root) { ; ,maxD ...

  5. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

  6. 104 Maximum Depth of Binary Tree 二叉树的最大深度

    给定一个二叉树,找出其最大深度.二叉树的深度为根节点到最远叶节点的最长路径上的节点数.案例:给出二叉树 [3,9,20,null,null,15,7],    3   / \  9  20    /  ...

  7. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  8. [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  9. (二叉树 BFS DFS) 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. [DP]Luogu 2014NOIP提高组 飞扬的小鸟题解

    2014NOIP提高组飞扬的小鸟题解 题目描述 Flappy Bird是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一 ...

  2. Kubernetes 企业级集群部署方式

    一.Kubernetes介绍与特性 1.1.kubernetes是什么 官方网站:http://www.kubernetes.io • Kubernetes是Google在2014年开源的一个容器集群 ...

  3. Microsoft.Extensions.DependencyInjection 之二:使用诊断工具观察内存占用

    目录 准备工作 大量接口与实现类的生成 elasticsearch+kibana+apm asp.net core 应用 请求与快照 Kibana 上的请求记录 请求耗时的分析 请求内存的分析 第2次 ...

  4. CSS教程详解

    CSS学习笔记 一.CSS基础 1.CSS简介 层叠:一层一层的: 样式表:很多的属性和样式 CSS语法: <style> 选择器 { 属性名:属性值; 属性名:属性值; ……  } &l ...

  5. NRF24L01双向无线通信

    最近闲来无事,利用手头资源研究了一下基于nrf24L01的双向通信实验,整个系统如下图所示. 原理: nrf24L01本身是一种单向通信的无线模块,但是,当nrf24L01工作在增强型的 ShockB ...

  6. Python破解js加密实例(有道在线翻译)

    在爬虫爬取网站的时候,经常遇到一些反爬虫技术,比如: 加cookie,身份验证UserAgent 图形验证,还有很难破解的滑动验证 js签名验证,对传输数据进行加密处理 对于js加密经过加密传输的就是 ...

  7. 开发技术--浅谈Python函数

    开发|浅谈Python函数 函数在实际使用中有很多不一样的小九九,我将从最基础的函数内容,延伸出函数的高级用法.此文非科普片~~ 前言 目前所有的文章思想格式都是:知识+情感. 知识:对于所有的知识点 ...

  8. ca动画

    //动画上下文-(void)animationOfUIKit{    UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 1 ...

  9. 解决jmeter进行分布式测试,远程机器来运行脚本,在察看结果树中的响应数据项为空白

    下面为大家提供一个解决办法: 第一步:打开主控机的jmeter--bin目录下的jmeter.properties文件 第二步:查找到mode=Standard 项 第三步:将其前边的注释去掉,然后保 ...

  10. 【已解决】git怎么合并多个分支到主干master

    git支持很多种工作流程,我们采用的一般是这样,远程创建一个主分支,本地每人创建功能分支,日常工作流程如下: 去自己的工作分支$ git checkout workbranch 工作.... 提交工作 ...