Skip to content

Commit ef7a87e

Browse files
committed
三刷94
1 parent 0c3d978 commit ef7a87e

File tree

7 files changed

+131
-15
lines changed

7 files changed

+131
-15
lines changed

docs/0000-data-structure-tree.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ image::images/0000-ds-tree-02.gif[{image_attr}]
4040
. 归并排序和二叉树后根遍历的递归顺序是一样的。
4141

4242

43+
44+
[#morris-traversal]
4345
== Morris 遍历
4446

4547
二叉搜索树相关的的一些题目,很可能就会利用中序遍历是升序序列的特性来处理一下问题。那么,在时间复杂度相同,但空间复杂度都特别优秀的 Morris 遍历就是一个很好的选择。
@@ -49,6 +51,8 @@ image::images/0000-ds-tree-01.jpg[{image_attr}]
4951
. xref:0098-validate-binary-search-tree.adoc[98. Validate Binary Search Tree]
5052
. xref:0099-recover-binary-search-tree.adoc[99. Recover Binary Search Tree]
5153

54+
在 https://leetcode.cn/problems/binary-tree-inorder-traversal/solutions/96765/dong-hua-yan-shi-94-er-cha-shu-de-zhong-xu-bian-li/[94. 二叉树的中序遍历 - 动画演示+三种实现^] 中的“变形”莫尔斯算法则展示了莫尔斯算法的另一种 有趣用法。
55+
5256
== 树形 DP 套路
5357

5458
树形 DP 套路使用前提:如果题目求解目标是 S 规则,则求解流程可以定成以每一个节点为头节点的子树在 S 规则下的每一个答案,并且最终答案一定在其中。

docs/0094-binary-tree-inorder-traversal.adoc

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
11
[#0094-binary-tree-inorder-traversal]
2-
= 94. Binary Tree Inorder Traversal
2+
= 94. 二叉树的中序遍历
3+
4+
https://leetcode.cn/problems/binary-tree-inorder-traversal/[LeetCode - 94. 二叉树的中序遍历^]
5+
6+
给定一个二叉树的根节点 `root` ,返回 _它的 *中序* 遍历_
7+
8+
*示例 1:*
9+
10+
image::images/0094-01.jpg[{image_attr}]
11+
12+
....
13+
输入:root = [1,null,2,3]
14+
输出:[1,3,2]
15+
....
16+
17+
*示例 2:*
18+
19+
....
20+
输入:root = []
21+
输出:[]
22+
....
23+
24+
*示例 3:*
25+
26+
....
27+
输入:root = [1]
28+
输出:[1]
29+
....
30+
31+
*提示:*
32+
33+
* 树中节点数目在范围 `[0, 100]`
34+
* `+-100 <= Node.val <= 100+`
35+
36+
*进阶:* 递归算法很简单,你可以通过迭代算法完成吗?
37+
38+
39+
== 思路分析
340

4-
{leetcode}/problems/binary-tree-inorder-traversal/[LeetCode - Binary Tree Inorder Traversal^]
541

642
迭代的方式,还不是很理解,还需要再思考思考。
743

8-
Given a binary tree, return the _inorder_ traversal of its nodes' values.
44+
迭代的办法就是用栈来模拟递归:有左节点就入栈;没有左节点就出栈,然后把有节点再入栈;以此类推。注意,入栈左右子树节点时,要把上下级的关联给斩断,否则会造成死循环。
945

10-
*Example:*
46+
TIP: 看官方题解,如果使用两层循环就不需要改变树的结构。自己想一想怎么实现?
1147

12-
[subs="verbatim,quotes,macros"]
13-
----
14-
*Input:* [1,null,2,3]
15-
1
16-
\
17-
2
18-
/
19-
3
48+
image::images/0094-10.gif[{image_attr}]
2049

21-
*Output:* [1,3,2]
22-
----
50+
“变形”莫里斯算法也是非常有趣:
2351

24-
*Follow up:* Recursive solution is trivial, could you do it iteratively?
52+
image::images/0094-11.gif[{image_attr}]
2553

54+
NOTE: 详细介绍见: <<morris-traversal>>。
2655

2756
[[src-0094]]
2857
[tabs]
@@ -53,5 +82,20 @@ include::{sourcedir}/_0094_BinaryTreeInorderTraversal_Recur.java[tag=answer]
5382
include::{sourcedir}/_0094_BinaryTreeInorderTraversal_Stack.java[tag=answer]
5483
----
5584
--
85+
86+
三刷::
87+
+
88+
--
89+
[{java_src_attr}]
90+
----
91+
include::{sourcedir}/_0094_BinaryTreeInorderTraversal_3.java[tag=answer]
92+
----
93+
--
5694
====
5795

96+
97+
== 参考资料
98+
99+
. https://leetcode.cn/problems/binary-tree-inorder-traversal/solutions/25220/yan-se-biao-ji-fa-yi-chong-tong-yong-qie-jian-ming/[94. 二叉树的中序遍历 - 颜色标记法,一种通用且简明的树遍历方法^]
100+
. https://leetcode.cn/problems/binary-tree-inorder-traversal/solutions/96765/dong-hua-yan-shi-94-er-cha-shu-de-zhong-xu-bian-li/[94. 二叉树的中序遍历 - 动画演示+三种实现^]
101+
. https://leetcode.cn/problems/binary-tree-inorder-traversal/solutions/412886/er-cha-shu-de-zhong-xu-bian-li-by-leetcode-solutio/[94. 二叉树的中序遍历 - 官方题解^]

docs/images/0094-01.jpg

8.45 KB
Loading

docs/images/0094-10.gif

342 KB
Loading

docs/images/0094-11.gif

113 KB
Loading

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,6 +1904,11 @@ endif::[]
19041904
|{doc_base_url}/0098-validate-binary-search-tree.adoc[题解]
19051905
|✅ 树形DP套路或中序遍历
19061906

1907+
|{counter:codes2503}
1908+
|{leetcode_base_url}/binary-tree-inorder-traversal/[94. 二叉树的中序遍历^]
1909+
|{doc_base_url}/0094-binary-tree-inorder-traversal.adoc[题解]
1910+
|✅ 深度优先+栈。递归法非常简单;迭代法则是用栈来模拟递归;而“变形”莫尔斯解法则非常有趣。
1911+
19071912
|===
19081913

19091914
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
import com.diguage.util.TreeNodes;
5+
6+
import java.util.*;
7+
8+
public class _0094_BinaryTreeInorderTraversal_3 {
9+
// tag::answer[]
10+
11+
/**
12+
* @author D瓜哥 · https://www.diguage.com
13+
* @since 2025-11-13 23:12:08
14+
*/
15+
public List<Integer> inorderTraversal(TreeNode root) {
16+
if (root == null) {
17+
return Collections.emptyList();
18+
}
19+
List<Integer> result = new ArrayList<>();
20+
Deque<TreeNode> stack = new LinkedList<>();
21+
stack.push(root);
22+
while (!stack.isEmpty()) {
23+
TreeNode peek = stack.peek();
24+
if (peek.left != null) {
25+
stack.push(peek.left);
26+
peek.left = null;
27+
} else {
28+
TreeNode pop = stack.pop();
29+
result.add(pop.val);
30+
if (pop.right != null) {
31+
stack.push(pop.right);
32+
pop.right = null;
33+
}
34+
}
35+
}
36+
return result;
37+
}
38+
// end::answer[]
39+
40+
static void main() {
41+
new _0094_BinaryTreeInorderTraversal_3()
42+
.inorderTraversal(TreeNodes.buildTree(Arrays.asList(1, 2, 3, 4, 5, null, 8, null, null, 6, 7, 9)));
43+
}
44+
45+
// }
46+
// public List<Integer> inorderTraversal(TreeNode root) {
47+
// if (root == null) {
48+
// return Collections.emptyList();
49+
// }
50+
// List<Integer> result = new ArrayList<>();
51+
// inorder(root, result);
52+
// return result;
53+
// }
54+
//
55+
// private void inorder(TreeNode root, List<Integer> result) {
56+
// if (root == null) {
57+
// return;
58+
// }
59+
// inorder(root.left, result);
60+
// result.add(root.val);
61+
// inorder(root.right, result);
62+
// }
63+
}

0 commit comments

Comments
 (0)