#yyds干货盘点# LeetCode 热题 HOT 100:二叉树的最大深度

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#yyds干货盘点# LeetCode 热题 HOT 100:二叉树的最大深度相关的知识,希望对你有一定的参考价值。

题目:

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],

   3

  / \\

 9  20

   /  \\

  15   7

返回它的最大深度 3 。

代码实现:

class Solution 
public int maxDepth(TreeNode root)
if (root == null)
return 0;

Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
int ans = 0;
while (!queue.isEmpty())
int size = queue.size();
while (size > 0)
TreeNode node = queue.poll();
if (node.left != null)
queue.offer(node.left);

if (node.right != null)
queue.offer(node.right);

size--;

ans++;

return ans;

以上是关于#yyds干货盘点# LeetCode 热题 HOT 100:二叉树的最大深度的主要内容,如果未能解决你的问题,请参考以下文章

#yyds干货盘点# LeetCode 热题 HOT 100:最长有效括号

#yyds干货盘点# LeetCode 热题 HOT 100:对称二叉树

#yyds干货盘点# LeetCode 热题 HOT 100:旋转图像

#yyds干货盘点# LeetCode 热题 HOT 100:单词搜索

#yyds干货盘点# LeetCode 热题 HOT 100:组合总和

#yyds干货盘点# LeetCode 热题 HOT 100:接雨水