Skip to content

Commit c92de40

Browse files
committed
三刷226
1 parent 0b56fc3 commit c92de40

File tree

8 files changed

+83
-28
lines changed

8 files changed

+83
-28
lines changed

docs/0226-invert-binary-tree.adoc

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,56 @@
11
[#0226-invert-binary-tree]
2-
= 226. Invert Binary Tree
2+
= 226. 翻转二叉树
33

4-
{leetcode}/problems/invert-binary-tree/[LeetCode - Invert Binary Tree^]
4+
https://leetcode.cn/problems/invert-binary-tree/[LeetCode - 226. 翻转二叉树^]
55

6-
Invert a binary tree.
6+
给你一棵二叉树的根节点 `root`,翻转这棵二叉树,并返回其根节点。
77

8-
.Example:
9-
----
10-
Input:
11-
12-
4
13-
/ \
14-
2 7
15-
/ \ / \
16-
1 3 6 9
17-
18-
Output:
19-
4
20-
/ \
21-
7 2
22-
/ \ / \
23-
9 6 3 1
24-
----
8+
*示例 1:*
9+
10+
image::images/0226-01.jpg[{image_attr}]
11+
12+
....
13+
输入:root = [4,2,7,1,3,6,9]
14+
输出:[4,7,2,9,6,3,1]
15+
....
16+
17+
*示例 2:*
2518

26-
*Trivia:*
19+
image::images/0226-02.jpg[{image_attr}]
2720

28-
This problem was inspired by https://twitter.com/mxcl/status/608682016205344768[this original tweet] by https://twitter.com/mxcl[Max Howell (@mxcl)^]:
21+
....
22+
输入:root = [2,1,3]
23+
输出:[2,3,1]
24+
....
2925

30-
****
31-
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
32-
****
26+
*示例 3:*
27+
28+
....
29+
输入:root = []
30+
输出:[]
31+
....
32+
33+
*提示:*
34+
35+
* 树中节点数目范围在 `[0, 100]`
36+
* `+-100 <= Node.val <= 100+`
3337
3438
3539
== 思路分析
3640

3741
D瓜哥也不会做这道题,现在可以说跟大神一个水平了,😆
3842

43+
image::images/0226-10.png[{image_attr}]
44+
45+
TIP: 现在D瓜哥可以顺利做出这道题,水平稳超大神,😁😁
46+
3947
其实,思路很简单:就是递归地反转每棵树即可。
4048

41-
image::images/0226-01.png[{image_attr}]
49+
image::images/0226-11.png[{image_attr}]
50+
51+
动图演示如下:
52+
53+
image::images/0226-12.gif[{image_attr}]
4254

4355
[[src-0226]]
4456
[tabs]
@@ -60,9 +72,22 @@ include::{sourcedir}/_0226_InvertBinaryTree.java[tag=answer]
6072
include::{sourcedir}/_0226_InvertBinaryTree_2.java[tag=answer]
6173
----
6274
--
75+
76+
三刷::
77+
+
78+
--
79+
[{java_src_attr}]
80+
----
81+
include::{sourcedir}/_0226_InvertBinaryTree_3.java[tag=answer]
82+
----
83+
--
6384
====
6485

86+
6587
== 参考资料
6688

67-
. https://leetcode.cn/problems/invert-binary-tree/solutions/415160/fan-zhuan-er-cha-shu-by-leetcode-solution/?envType=study-plan-v2&envId=selected-coding-interview[226. 翻转二叉树 - 官方题解^]
68-
. https://leetcode.cn/problems/invert-binary-tree/solutions/2361621/226-fan-zhuan-er-cha-shu-fen-zhi-qing-xi-tqlf/?envType=study-plan-v2&envId=selected-coding-interview[226. 翻转二叉树 - 深度优先搜索,清晰图解^]
89+
. https://leetcode.cn/problems/invert-binary-tree/solutions/2713610/shi-pin-shen-ru-li-jie-di-gui-pythonjava-zhqh/[226. 翻转二叉树 - 两种递归写法^]
90+
. https://leetcode.cn/problems/invert-binary-tree/solutions/73159/dong-hua-yan-shi-liang-chong-shi-xian-226-fan-zhua/[226. 翻转二叉树 - 动画演示 两种实现^]
91+
. https://leetcode.cn/problems/invert-binary-tree/solutions/415507/shou-hua-tu-jie-san-chong-xie-fa-di-gui-liang-chon/[226. 翻转二叉树 - 「手画图解」剖析Howell大神没写出的面试题^]
92+
. https://leetcode.cn/problems/invert-binary-tree/solutions/415160/fan-zhuan-er-cha-shu-by-leetcode-solution/[226. 翻转二叉树 - 官方题解^]
93+
. https://leetcode.cn/problems/invert-binary-tree/solutions/2361621/226-fan-zhuan-er-cha-shu-fen-zhi-qing-xi-tqlf/[226. 翻转二叉树 - 深度优先搜索,清晰图解^]

docs/images/0226-01.jpg

33.5 KB
Loading

docs/images/0226-02.jpg

14.7 KB
Loading

docs/images/0226-10.png

52.6 KB
Loading
File renamed without changes.

docs/images/0226-12.gif

446 KB
Loading

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,11 @@ endif::[]
17891789
|{doc_base_url}/0199-binary-tree-right-side-view.adoc[题解]
17901790
|✅ 深度优先遍历。可以不用 `Map`,直接用 `List` 即可。要注意**先根遍历****中根遍历**的区别:先根遍历每层都是按顺序从上到下添加到 `List`;而中根遍历,每次都是最下层到,所以,要添加 `null` 占位符把 `List` 给撑起来,后续直接按照坐标设置。
17911791

1792+
|{counter:codes2503}
1793+
|{leetcode_base_url}/invert-binary-tree/[226. 翻转二叉树^]
1794+
|{doc_base_url}/0226-invert-binary-tree.adoc[题解]
1795+
|✅ 深度优先遍历或广度优先遍历。先递归再交换,或者先交换再递归。
1796+
17921797
|===
17931798

17941799
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import com.diguage.algo.util.TreeNode;
4+
5+
import java.util.Objects;
6+
7+
public class _0226_InvertBinaryTree_3 {
8+
// tag::answer[]
9+
/**
10+
* @author D瓜哥 · https://www.diguage.com
11+
* @since 2025-10-26 20:41:17
12+
*/
13+
public TreeNode invertTree(TreeNode root) {
14+
if (Objects.isNull(root)) {
15+
return root;
16+
}
17+
TreeNode left = invertTree(root.left);
18+
TreeNode right = invertTree(root.right);
19+
root.left = right; // 交换左右子树
20+
root.right = left;
21+
return root;
22+
}
23+
// end::answer[]
24+
25+
}

0 commit comments

Comments
 (0)