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],

    3
/ \
9 20
/ \
15 7

return its depth = 3.

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

C++ 解法一:

class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root) return ;
return + max(maxDepth(root->left), maxDepth(root->right));
}
};

Java 解法一:

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

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

C++ 解法二:

class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root) return ;
int res = ;
queue<TreeNode*> q{{root}};
while (!q.empty()) {
++res;
for (int i = q.size(); i > ; --i) {
TreeNode *t = q.front(); q.pop();
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
}
}
return res;
}
};

Java 解法二:

public class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
int res = 0;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while (!q.isEmpty()) {
++res;
for (int i = q.size(); i > 0; --i) {
TreeNode t = q.poll();
if (t.left != null) q.offer(t.left);
if (t.right != null) q.offer(t.right);
}
}
return res;
}
}

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. MongoDB副本集--Secondary节点实例恢复

    场景描述 MongoDB副本集中有一台Secondary节点出现RECOVERING的状态 状态如下: arps:RECOVERING> rs.status() { "set" ...

  2. SAP PI开发手册-ERP发布服务供外围系统调用(RFC类型)

    1转自:https://www.cnblogs.com/fanjb/p/10677018.html 8年进入国网项目后陆陆续续做了一些接口,按实现方法去分有RFC和代理类sproxy类型,按服务提供方 ...

  3. C# iText split PDF C# 拆分PDF

    Nuget install iText7 using iText.Kernel.Pdf; using System.Linq; using System.Text; using System.Thre ...

  4. JVM 学习总结

    目录 Java内存区域 运行时数据区 & Java 内存结构 & Java 内存区域 1. 程序计数器 2. Java 虚拟机栈 3. 本地方法栈 4. 堆 5. 方法区 6. 运行时 ...

  5. iOS开发之--iPhone X 适配:MJRefresh上拉加载适配

    问题如下图: 出现原因,phoneX系列手机下方多了34像素的工作区域,所以需要对x全系列手机坐下适配, 解决如下: self.tableView.mj_footer.ignoredScrollVie ...

  6. RDD源码分析

    RDD源码解析 一. RDD.scala - Resilient Distributed Dataset (RDD) 弹性分布式数据集 弹性: 体现在计算上面 - the basic abstract ...

  7. 一不小心把windows资源管理器给结束任务了 电脑黑屏了 怎么处理

    按键盘上的三个键,Ctrl+Shift+Esc来启动任务管理器: 在任务管理器界面的左上角,有一个“文件”按钮,点击它会出现下拉的“新建任务(运行)...”按钮 鼠标点了“新建任务(运行)...”会弹 ...

  8. MarkdownPad 2破解

    MarkdownPad 2 是一款较不错的Markdown编辑器,可快速将文本转换为美观的HTML/XHTML的网页格式代码,且操作方便,用户可以通过键盘快捷键和工具栏按钮来使用或者移除Markdow ...

  9. Jenkins-Master-slave架构(八)

    一.增加slave节点 1.1 查看当前节点 系统管理-节点管理  1.2 新建节点  1.3 配置节点信息 可以选择只允许运行绑定到这台机器的job  1.4 保存后,使节点上线即可. 二.配置任务 ...

  10. CentOS最小化安装后找不到ifconfig命令

    1.ifconfig命令是设置或显示网络接口的程序,可以显示出我们机器的网卡信息, 可是有些时候最小化安装CentOS等Linux发行版的时候会默认不安装ifconfig等命令, 这时候你进入终端,运 ...