Skip to content

Commit 530fef7

Browse files
committed
补全第 100 ~ 199 题的题目解析(增加 12 道题)
1 parent fcf8225 commit 530fef7

17 files changed

+1464
-15
lines changed

docs/00_preface/00_05_solutions_list.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LeetCode 题解(已完成 874 道)
1+
# LeetCode 题解(已完成 886 道)
22

33
### 第 1 ~ 99 题
44

@@ -118,10 +118,12 @@
118118
| [0106. 从中序与后序遍历序列构造二叉树](https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/construct-binary-tree-from-inorder-and-postorder-traversal.md) | 树、数组、哈希表、分治、二叉树 | 中等 |
119119
| [0107. 二叉树的层序遍历 II](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/binary-tree-level-order-traversal-ii.md) | 树、广度优先搜索、二叉树 | 中等 |
120120
| [0108. 将有序数组转换为二叉搜索树](https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/convert-sorted-array-to-binary-search-tree.md) | 树、二叉搜索树、数组、分治、二叉树 | 简单 |
121+
| [0109. 有序链表转换二叉搜索树](https://leetcode.cn/problems/convert-sorted-list-to-binary-search-tree/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/convert-sorted-list-to-binary-search-tree.md) | 树、二叉搜索树、链表、分治、二叉树 | 中等 |
121122
| [0110. 平衡二叉树](https://leetcode.cn/problems/balanced-binary-tree/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/balanced-binary-tree.md) | 树、深度优先搜索、二叉树 | 简单 |
122123
| [0111. 二叉树的最小深度](https://leetcode.cn/problems/minimum-depth-of-binary-tree/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/minimum-depth-of-binary-tree.md) | 树、深度优先搜索、广度优先搜索、二叉树 | 简单 |
123124
| [0112. 路径总和](https://leetcode.cn/problems/path-sum/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/path-sum.md) | 树、深度优先搜索、广度优先搜索、二叉树 | 简单 |
124125
| [0113. 路径总和 II](https://leetcode.cn/problems/path-sum-ii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/path-sum-ii.md) | 树、深度优先搜索、回溯、二叉树 | 中等 |
126+
| [0114. 二叉树展开为链表](https://leetcode.cn/problems/flatten-binary-tree-to-linked-list/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/flatten-binary-tree-to-linked-list.md) | 栈、树、深度优先搜索、链表、二叉树 | 中等 |
125127
| [0115. 不同的子序列](https://leetcode.cn/problems/distinct-subsequences/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/distinct-subsequences.md) | 字符串、动态规划 | 困难 |
126128
| [0116. 填充每个节点的下一个右侧节点指针](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/populating-next-right-pointers-in-each-node.md) | 树、深度优先搜索、广度优先搜索、链表、二叉树 | 中等 |
127129
| [0117. 填充每个节点的下一个右侧节点指针 II](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/populating-next-right-pointers-in-each-node-ii.md) | 树、深度优先搜索、广度优先搜索、链表、二叉树 | 中等 |
@@ -133,11 +135,13 @@
133135
| [0123. 买卖股票的最佳时机 III](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/best-time-to-buy-and-sell-stock-iii.md) | 数组、动态规划 | 困难 |
134136
| [0124. 二叉树中的最大路径和](https://leetcode.cn/problems/binary-tree-maximum-path-sum/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/binary-tree-maximum-path-sum.md) | 树、深度优先搜索、动态规划、二叉树 | 困难 |
135137
| [0125. 验证回文串](https://leetcode.cn/problems/valid-palindrome/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/valid-palindrome.md) | 双指针、字符串 | 简单 |
138+
| [0126. 单词接龙 II](https://leetcode.cn/problems/word-ladder-ii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/word-ladder-ii.md) | 广度优先搜索、哈希表、字符串、回溯 | 困难 |
136139
| [0127. 单词接龙](https://leetcode.cn/problems/word-ladder/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/word-ladder.md) | 广度优先搜索、哈希表、字符串 | 困难 |
137140
| [0128. 最长连续序列](https://leetcode.cn/problems/longest-consecutive-sequence/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/longest-consecutive-sequence.md) | 并查集、数组、哈希表 | 中等 |
138141
| [0129. 求根节点到叶节点数字之和](https://leetcode.cn/problems/sum-root-to-leaf-numbers/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/sum-root-to-leaf-numbers.md) | 树、深度优先搜索、二叉树 | 中等 |
139142
| [0130. 被围绕的区域](https://leetcode.cn/problems/surrounded-regions/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/surrounded-regions.md) | 深度优先搜索、广度优先搜索、并查集、数组、矩阵 | 中等 |
140143
| [0131. 分割回文串](https://leetcode.cn/problems/palindrome-partitioning/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/palindrome-partitioning.md) | 字符串、动态规划、回溯 | 中等 |
144+
| [0132. 分割回文串 II](https://leetcode.cn/problems/palindrome-partitioning-ii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/palindrome-partitioning-ii.md) | 字符串、动态规划 | 困难 |
141145
| [0133. 克隆图](https://leetcode.cn/problems/clone-graph/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/clone-graph.md) | 深度优先搜索、广度优先搜索、图、哈希表 | 中等 |
142146
| [0134. 加油站](https://leetcode.cn/problems/gas-station/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/gas-station.md) | 贪心、数组 | 中等 |
143147
| [0135. 分发糖果](https://leetcode.cn/problems/candy/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/candy.md) | 贪心、数组 | 困难 |
@@ -151,6 +155,7 @@
151155
| [0143. 重排链表](https://leetcode.cn/problems/reorder-list/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/reorder-list.md) | 栈、递归、链表、双指针 | 中等 |
152156
| [0144. 二叉树的前序遍历](https://leetcode.cn/problems/binary-tree-preorder-traversal/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/binary-tree-preorder-traversal.md) | 栈、树、深度优先搜索、二叉树 | 简单 |
153157
| [0145. 二叉树的后序遍历](https://leetcode.cn/problems/binary-tree-postorder-traversal/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/binary-tree-postorder-traversal.md) | 栈、树、深度优先搜索、二叉树 | 简单 |
158+
| [0146. LRU 缓存](https://leetcode.cn/problems/lru-cache/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/lru-cache.md) | 设计、哈希表、链表、双向链表 | 中等 |
154159
| [0147. 对链表进行插入排序](https://leetcode.cn/problems/insertion-sort-list/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/insertion-sort-list.md) | 链表、排序 | 中等 |
155160
| [0148. 排序链表](https://leetcode.cn/problems/sort-list/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/sort-list.md) | 链表、双指针、分治、排序、归并排序 | 中等 |
156161
| [0149. 直线上最多的点数](https://leetcode.cn/problems/max-points-on-a-line/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/max-points-on-a-line.md) | 几何、数组、哈希表、数学 | 困难 |
@@ -160,10 +165,14 @@
160165
| [0153. 寻找旋转排序数组中的最小值](https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/find-minimum-in-rotated-sorted-array.md) | 数组、二分查找 | 中等 |
161166
| [0154. 寻找旋转排序数组中的最小值 II](https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array-ii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/find-minimum-in-rotated-sorted-array-ii.md) | 数组、二分查找 | 困难 |
162167
| [0155. 最小栈](https://leetcode.cn/problems/min-stack/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/min-stack.md) | 栈、设计 | 中等 |
168+
| [0156. 上下翻转二叉树](https://leetcode.cn/problems/binary-tree-upside-down/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/binary-tree-upside-down.md) | 树、深度优先搜索、二叉树 | 中等 |
163169
| [0159. 至多包含两个不同字符的最长子串](https://leetcode.cn/problems/longest-substring-with-at-most-two-distinct-characters/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/longest-substring-with-at-most-two-distinct-characters.md) | 哈希表、字符串、滑动窗口 | 中等 |
164170
| [0160. 相交链表](https://leetcode.cn/problems/intersection-of-two-linked-lists/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/intersection-of-two-linked-lists.md) | 哈希表、链表、双指针 | 简单 |
171+
| [0161. 相隔为 1 的编辑距离](https://leetcode.cn/problems/one-edit-distance/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/one-edit-distance.md) | 双指针、字符串 | 中等 |
165172
| [0162. 寻找峰值](https://leetcode.cn/problems/find-peak-element/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/find-peak-element.md) | 数组、二分查找 | 中等 |
173+
| [0163. 缺失的区间](https://leetcode.cn/problems/missing-ranges/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/missing-ranges.md) | 数组 | 简单 |
166174
| [0164. 最大间距](https://leetcode.cn/problems/maximum-gap/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/maximum-gap.md) | 数组、桶排序、基数排序、排序 | 中等 |
175+
| [0165. 比较版本号](https://leetcode.cn/problems/compare-version-numbers/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/compare-version-numbers.md) | 双指针、字符串 | 中等 |
167176
| [0166. 分数到小数](https://leetcode.cn/problems/fraction-to-recurring-decimal/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/fraction-to-recurring-decimal.md) | 哈希表、数学、字符串 | 中等 |
168177
| [0167. 两数之和 II - 输入有序数组](https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/two-sum-ii-input-array-is-sorted.md) | 数组、双指针、二分查找 | 中等 |
169178
| [0168. Excel 表列名称](https://leetcode.cn/problems/excel-sheet-column-title/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/excel-sheet-column-title.md) | 数学、字符串 | 简单 |
@@ -172,7 +181,10 @@
172181
| [0171. Excel 表列序号](https://leetcode.cn/problems/excel-sheet-column-number/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/excel-sheet-column-number.md) | 数学、字符串 | 简单 |
173182
| [0172. 阶乘后的零](https://leetcode.cn/problems/factorial-trailing-zeroes/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/factorial-trailing-zeroes.md) | 数学 | 中等 |
174183
| [0173. 二叉搜索树迭代器](https://leetcode.cn/problems/binary-search-tree-iterator/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/binary-search-tree-iterator.md) | 栈、树、设计、二叉搜索树、二叉树、迭代器 | 中等 |
184+
| [0174. 地下城游戏](https://leetcode.cn/problems/dungeon-game/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/dungeon-game.md) | 数组、动态规划、矩阵 | 困难 |
175185
| [0179. 最大数](https://leetcode.cn/problems/largest-number/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/largest-number.md) | 贪心、数组、字符串、排序 | 中等 |
186+
| [0186. 反转字符串中的单词 II](https://leetcode.cn/problems/reverse-words-in-a-string-ii/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/reverse-words-in-a-string-ii.md) | 双指针、字符串 | 中等 |
187+
| [0187. 重复的DNA序列](https://leetcode.cn/problems/repeated-dna-sequences/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/repeated-dna-sequences.md) | 位运算、哈希表、字符串、滑动窗口、哈希函数、滚动哈希 | 中等 |
176188
| [0188. 买卖股票的最佳时机 IV](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iv/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/best-time-to-buy-and-sell-stock-iv.md) | 数组、动态规划 | 困难 |
177189
| [0189. 轮转数组](https://leetcode.cn/problems/rotate-array/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/rotate-array.md) | 数组、数学、双指针 | 中等 |
178190
| [0190. 颠倒二进制位](https://leetcode.cn/problems/reverse-bits/) | [题解](https://github.com/ITCharge/AlgoNote/tree/main/docs/solutions/0100-0199/reverse-bits.md) | 位运算、分治 | 简单 |

docs/others/update_time.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 2025-10
22

3+
- 2025-10-17 补全第 100 ~ 199 题的题目解析(增加 12 道题)
34
- 2025-10-16 补全第 1 ~ 99 题的题目解析(增加 13 道题)
45
- 2025-10-13 编写生成 PDF 脚本,并生成 AlgoNote-v2.0.pdf
56
- 2025-10-09 修复内容显示问题
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# [0156. 上下翻转二叉树](https://leetcode.cn/problems/binary-tree-upside-down/)
2+
3+
- 标签:树、深度优先搜索、二叉树
4+
- 难度:中等
5+
6+
## 题目链接
7+
8+
- [0156. 上下翻转二叉树 - 力扣](https://leetcode.cn/problems/binary-tree-upside-down/)
9+
10+
## 题目大意
11+
12+
**描述**
13+
14+
给定一个二叉树的根节点 $root$。
15+
16+
**要求**
17+
18+
将此二叉树上下翻转,并返回新的根节点。
19+
20+
你可以按下面的步骤翻转一棵二叉树:
21+
22+
1. 原来的左子节点变成新的根节点
23+
2. 原来的根节点变成新的右子节点
24+
3. 原来的右子节点变成新的左子节点
25+
26+
![](https://assets.leetcode.com/uploads/2020/08/29/main.jpg)
27+
28+
上面的步骤逐层进行。题目数据保证每个右节点都有一个同级节点(即共享同一父节点的左节点)且不存在子节点。
29+
30+
**说明**
31+
32+
- 树中节点数目在范围 $[0, 10]$ 内。
33+
- $1 \le Node.val \le 10$。
34+
- 树中的每个右节点都有一个同级节点(即共享同一父节点的左节点)。
35+
- 树中的每个右节点都没有子节点。
36+
37+
**示例**
38+
39+
- 示例 1:
40+
41+
![](https://assets.leetcode.com/uploads/2020/08/29/updown.jpg)
42+
43+
```python
44+
输入:root = [1,2,3,4,5]
45+
输出:[4,5,2,null,null,3,1]
46+
```
47+
48+
- 示例 2:
49+
50+
```python
51+
输入:root = []
52+
输出:[]
53+
```
54+
55+
## 解题思路
56+
57+
### 思路 1:递归
58+
59+
根据题目描述,我们需要将二叉树进行上下翻转。观察翻转规则:
60+
61+
1. 原来的左子节点变成新的根节点
62+
2. 原来的根节点变成新的右子节点
63+
3. 原来的右子节点变成新的左子节点
64+
65+
这是一个递归问题,我们可以采用以下策略:
66+
67+
1. **递归终止条件**:如果当前节点 $root$ 为空或者没有左子节点,直接返回当前节点。
68+
2. **递归处理**:对左子树进行翻转,得到新的根节点 $new\_root$。
69+
3. **重新连接**
70+
- 将原根节点 $root$ 作为新根节点 $new\_root$ 的右子节点
71+
- 将原右子节点 $root.right$ 作为新根节点 $new\_root$ 的左子节点
72+
- 将原根节点 $root$ 的左右子节点设为 $null$,避免循环引用
73+
74+
**关键点**
75+
- 翻转后的新根节点是原树最左边的叶子节点
76+
- 需要保存原根节点和右子节点,用于重新连接
77+
- 翻转是逐层进行的,每层都遵循相同的翻转规则
78+
79+
### 思路 1:代码
80+
81+
```python
82+
# Definition for a binary tree node.
83+
# class TreeNode:
84+
# def __init__(self, val=0, left=None, right=None):
85+
# self.val = val
86+
# self.left = left
87+
# self.right = right
88+
class Solution:
89+
def upsideDownBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
90+
# 递归终止条件:空节点或没有左子节点
91+
if not root or not root.left:
92+
return root
93+
94+
# 保存原根节点和右子节点
95+
original_root = root
96+
original_right = root.right
97+
98+
# 递归处理左子树,得到新的根节点
99+
new_root = self.upsideDownBinaryTree(root.left)
100+
101+
# 重新连接节点
102+
# 原左子节点(现在是新根节点)的左子节点指向原右子节点
103+
root.left.left = original_right
104+
# 原左子节点(现在是新根节点)的右子节点指向原根节点
105+
root.left.right = original_root
106+
107+
# 将原根节点的左右子节点设为 None,避免循环引用
108+
original_root.left = None
109+
original_root.right = None
110+
111+
return new_root
112+
```
113+
114+
### 思路 1:复杂度分析
115+
116+
- **时间复杂度**:$O(h)$,其中 $h$ 是树的高度。我们需要递归到树的最左边叶子节点,递归深度等于树的高度。
117+
- **空间复杂度**:$O(h)$,其中 $h$ 是树的高度。递归调用栈的深度等于树的高度。

0 commit comments

Comments
 (0)