

#yyds干货盘点# LeetCode 热题 HOT 100:二叉树的最大深度
source link: https://blog.51cto.com/u_13321676/5767510
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

#yyds干货盘点# LeetCode 热题 HOT 100:二叉树的最大深度
精选 原创给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
给定二叉树 [3,9,20,null,null,15,7],
返回它的最大深度 3 。
代码实现:
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;
}
}
Recommend
-
6
#yyds干货盘点# LeetCode 热题 HOT 100:两数相加 精选 原创 灰太狼_cxh 2022-09-...
-
4
#yyds干货盘点# LeetCode 热题 HOT 100:两数之和 精选 原创 灰太狼_cxh 2022-09-...
-
3
#yyds干货盘点# LeetCode 热题 HOT 100:无重复字符的最长子串 精选 原创 灰太狼_cxh
-
4
#yyds干货盘点# LeetCode 热题 HOT 100:电话号码的字母组合 精选 原创 灰太狼_cxh
-
4
#yyds干货盘点# LeetCode 热题 HOT 100:四数之和 精选 原创 灰太狼_cxh 2022-09-...
-
9
#yyds干货盘点# LeetCode 热题 HOT 100: 有效的括号 精选 原创 灰太狼_cxh 2022-...
-
12
#yyds干货盘点# LeetCode 热题 HOT 100:合并两个有序链表 精选 原创 灰太狼_cxh ...
-
7
#yyds干货盘点# LeetCode 热题 HOT 100:括号生成 精选 原创 灰太狼_cxh 2022-09-...
-
8
#yyds干货盘点# LeetCode 热题 HOT 100:最大子数组和 精选 原创 灰太狼_cxh 2022...
-
5
#yyds干货盘点# LeetCode 热题 HOT 100:验证二叉搜索树 精选 原创 灰太狼_cxh 20...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK