Skip to content

Commit 0b08d35

Browse files
committed
四刷104
1 parent 4a30d57 commit 0b08d35

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

docs/0104-maximum-depth-of-binary-tree.adoc

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
[#0104-maximum-depth-of-binary-tree]
2-
= 104. Maximum Depth of Binary Tree
2+
= 104. 二叉树的最大深度
33

4-
{leetcode}/problems/maximum-depth-of-binary-tree/[LeetCode - Maximum Depth of Binary Tree^]
4+
https://leetcode.cn/problems/maximum-depth-of-binary-tree/[LeetCode - 104. 二叉树的最大深度^]
55

6-
Given a binary tree, find its maximum depth.
6+
给定一个二叉树 `root` ,返回其最大深度。
77

8-
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
8+
二叉树的 *最大深度* 是指从根节点到最远叶子节点的最长路径上的节点数。
99

10-
*Note:* A leaf is a node with no children.
10+
*示例 1:*
1111

12-
*Example:*
12+
image::images/0104-01.jpg[{image_attr}]
1313

14-
Given binary tree `[3,9,20,null,null,15,7]`,
14+
....
15+
输入:root = [3,9,20,null,null,15,7]
16+
输出:3
17+
....
1518

16-
[subs="verbatim,quotes,macros"]
17-
----
18-
3
19-
/ \
20-
9 20
21-
/ \
22-
15 7
23-
----
19+
*示例 2:*
20+
21+
....
22+
输入:root = [1,null,2]
23+
输出:2
24+
....
25+
26+
*提示:*
27+
28+
* 树中节点的数量在 `[0, 10^4^]` 区间内。
29+
* `+-100 <= Node.val <= 100+`
2430
25-
return its depth = 3.
2631
2732
== 思路分析
2833

@@ -59,11 +64,21 @@ include::{sourcedir}/_0104_MaximumDepthOfBinaryTree_2.java[tag=answer]
5964
include::{sourcedir}/_0104_MaximumDepthOfBinaryTree_3.java[tag=answer]
6065
----
6166
--
67+
68+
四刷::
69+
+
70+
--
71+
[{java_src_attr}]
72+
----
73+
include::{sourcedir}/_0104_MaximumDepthOfBinaryTree_4.java[tag=answer]
74+
----
75+
--
6276
====
6377

6478
思考题:尝试使用迭代方式来解决一下。
6579

6680
== 参考资料
6781

68-
. https://leetcode.cn/problems/maximum-depth-of-binary-tree/solutions/349250/er-cha-shu-de-zui-da-shen-du-by-leetcode-solution/[104. 二叉树的最大深度 - 官方题解^]
82+
. https://leetcode.cn/problems/maximum-depth-of-binary-tree/solutions/2010612/kan-wan-zhe-ge-shi-pin-rang-ni-dui-di-gu-44uz/[104. 二叉树的最大深度 - 让你对递归的理解更上一层楼!^]
6983
. https://leetcode.cn/problems/maximum-depth-of-binary-tree/solutions/2361697/104-er-cha-shu-de-zui-da-shen-du-hou-xu-txzrx/[104. 二叉树的最大深度 -后序或层序遍历,清晰图解^]
84+
. https://leetcode.cn/problems/maximum-depth-of-binary-tree/solutions/349250/er-cha-shu-de-zui-da-shen-du-by-leetcode-solution/[104. 二叉树的最大深度 - 官方题解^]

docs/images/0104-01.jpg

12.5 KB
Loading
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
5+
public class _0104_MaximumDepthOfBinaryTree_4 {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-11-12 21:49:21
10+
*/
11+
public int maxDepth(TreeNode root) {
12+
if (null == root) {
13+
return 0;
14+
}
15+
int left = maxDepth(root.left);
16+
int right = maxDepth(root.right);
17+
return Math.max(left, right) + 1;
18+
}
19+
// end::answer[]
20+
}

0 commit comments

Comments
 (0)