Skip to content

Commit 36379a0

Browse files
committed
三刷104
1 parent ff4db53 commit 36379a0

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
{leetcode}/problems/maximum-depth-of-binary-tree/[LeetCode - Maximum Depth of Binary Tree^]
55

6-
思考题:尝试使用迭代方式来解决一下。
7-
86
Given a binary tree, find its maximum depth.
97

108
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
@@ -33,15 +31,37 @@ return its depth = 3.
3331
image::images/0104-01.png[{image_attr}]
3432

3533
[[src-0104]]
34+
[tabs]
35+
====
36+
一刷::
37+
+
38+
--
3639
[{java_src_attr}]
3740
----
3841
include::{sourcedir}/_0104_MaximumDepthOfBinaryTree.java[tag=answer]
3942
----
43+
--
4044
45+
二刷::
46+
+
47+
--
4148
[{java_src_attr}]
4249
----
4350
include::{sourcedir}/_0104_MaximumDepthOfBinaryTree_2.java[tag=answer]
4451
----
52+
--
53+
54+
三刷::
55+
+
56+
--
57+
[{java_src_attr}]
58+
----
59+
include::{sourcedir}/_0104_MaximumDepthOfBinaryTree_3.java[tag=answer]
60+
----
61+
--
62+
====
63+
64+
思考题:尝试使用迭代方式来解决一下。
4565

4666
== 参考资料
4767

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,11 @@
813813
|{doc_base_url}/0215-kth-largest-element-in-an-array.adoc[题解]
814814
|❌ 快速查找,知道思路,代码却无从下手。
815815

816+
|{counter:codes}
817+
|{leetcode_base_url}/maximum-depth-of-binary-tree/[104. Maximum Depth of Binary Tree^]
818+
|{doc_base_url}/0104-maximum-depth-of-binary-tree.adoc[题解]
819+
|✅ 递归+回溯
820+
816821
|===
817822

818823
截止目前,本轮练习一共完成 {codes} 道题。

src/main/java/com/diguage/algo/leetcode/_0104_MaximumDepthOfBinaryTree.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public class _0104_MaximumDepthOfBinaryTree {
4242
* Runtime: 1 ms, faster than 14.28% of Java online submissions for Maximum Depth of Binary Tree.
4343
*
4444
* Memory Usage: 40.7 MB, less than 5.38% of Java online submissions for Maximum Depth of Binary Tree.
45+
*
46+
* @author D瓜哥 · https://www.diguage.com
47+
* @since 2020-01-24 22:17
4548
*/
4649
public int maxDepth(TreeNode root) {
4750
if (Objects.isNull(root)) {

src/main/java/com/diguage/algo/leetcode/_0104_MaximumDepthOfBinaryTree_2.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public class _0104_MaximumDepthOfBinaryTree_2 {
3636
/**
3737
* 后根遍历既可以访问父节点,又可以从返回值中获取想要的信息,
3838
* 比如深度,子节点等等。
39+
*
40+
* @author D瓜哥 · https://www.diguage.com
41+
* @since 2024-06-30 22:09:09
3942
*/
4043
public int maxDepth(TreeNode root) {
4144
if (root == null) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
5+
public class _0104_MaximumDepthOfBinaryTree_3 {
6+
// tag::answer[]
7+
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2024-09-20 23:45:16
11+
*/
12+
public int maxDepth(TreeNode root) {
13+
if (root == null) {
14+
return 0;
15+
}
16+
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
17+
}
18+
// end::answer[]
19+
}

0 commit comments

Comments
 (0)