Skip to content

Commit 00d7613

Browse files
author
lucifer
committed
2 parents a8d575c + c4ce20f commit 00d7613

10 files changed

+76
-115
lines changed

problems/102.binary-tree-level-order-traversal.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,7 @@ class Solution:
202202
## 相关题目
203203
- [103.binary-tree-zigzag-level-order-traversal](./103.binary-tree-zigzag-level-order-traversal.md)
204204
- [104.maximum-depth-of-binary-tree](./104.maximum-depth-of-binary-tree.md)
205+
206+
## 相关专题
207+
208+
- [二叉树的遍历](https://github.com/azl397985856/leetcode/blob/master/thinkings/binary-tree-traversal.md)

problems/144.binary-tree-preorder-traversal.md

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ Follow up: Recursive solution is trivial, could you do it iteratively?
3333
前序遍历是`根左右`的顺序,注意是``开始,那么就很简单。直接先将根节点入栈,然后
3434
看有没有右节点,有则入栈,再看有没有左节点,有则入栈。 然后出栈一个元素,重复即可。
3535

36-
> 其他树的非递归遍历课没这么简单
36+
> 其他树的非递归遍历可没这么简单
37+
38+
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gg7v5hztd2j30zu0ntwiu.jpg)
39+
40+
(迭代 VS 递归)
3741

3842
## 关键点解析
3943

@@ -58,44 +62,6 @@ Follow up: Recursive solution is trivial, could you do it iteratively?
5862
JavaScript Code:
5963

6064
```js
61-
/*
62-
* @lc app=leetcode id=144 lang=javascript
63-
*
64-
* [144] Binary Tree Preorder Traversal
65-
*
66-
* https://leetcode.com/problems/binary-tree-preorder-traversal/description/
67-
*
68-
* algorithms
69-
* Medium (50.36%)
70-
* Total Accepted: 314K
71-
* Total Submissions: 621.2K
72-
* Testcase Example: '[1,null,2,3]'
73-
*
74-
* Given a binary tree, return the preorder traversal of its nodes' values.
75-
*
76-
* Example:
77-
*
78-
*
79-
* Input: [1,null,2,3]
80-
* ⁠ 1
81-
* ⁠ \
82-
* ⁠ 2
83-
* ⁠ /
84-
* ⁠ 3
85-
*
86-
* Output: [1,2,3]
87-
*
88-
*
89-
* Follow up: Recursive solution is trivial, could you do it iteratively?
90-
*
91-
*/
92-
/**
93-
* Definition for a binary tree node.
94-
* function TreeNode(val) {
95-
* this.val = val;
96-
* this.left = this.right = null;
97-
* }
98-
*/
9965
/**
10066
* @param {TreeNode} root
10167
* @return {number[]}
@@ -115,13 +81,13 @@ var preorderTraversal = function(root) {
11581
let t = stack.pop();
11682

11783
while (t) {
118-
ret.push(t.val);
11984
if (t.right) {
12085
stack.push(t.right);
12186
}
12287
if (t.left) {
12388
stack.push(t.left);
12489
}
90+
ret.push(t.val);
12591
t = stack.pop();
12692
}
12793

@@ -159,3 +125,7 @@ public:
159125
}
160126
};
161127
```
128+
129+
## 相关专题
130+
131+
- [二叉树的遍历](https://github.com/azl397985856/leetcode/blob/master/thinkings/binary-tree-traversal.md)

problems/145.binary-tree-postorder-traversal.md

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,6 @@ mid是一个具体的节点,left和right`递归求出即可`
5959
## 代码
6060

6161
```js
62-
/*
63-
* @lc app=leetcode id=145 lang=javascript
64-
*
65-
* [145] Binary Tree Postorder Traversal
66-
*
67-
* https://leetcode.com/problems/binary-tree-postorder-traversal/description/
68-
*
69-
* algorithms
70-
* Hard (47.06%)
71-
* Total Accepted: 242.6K
72-
* Total Submissions: 512.8K
73-
* Testcase Example: '[1,null,2,3]'
74-
*
75-
* Given a binary tree, return the postorder traversal of its nodes' values.
76-
*
77-
* Example:
78-
*
79-
*
80-
* Input: [1,null,2,3]
81-
* ⁠ 1
82-
* ⁠ \
83-
* ⁠ 2
84-
* ⁠ /
85-
* ⁠ 3
86-
*
87-
* Output: [3,2,1]
88-
*
89-
*
90-
* Follow up: Recursive solution is trivial, could you do it iteratively?
91-
*
92-
*/
9362
/**
9463
* Definition for a binary tree node.
9564
* function TreeNode(val) {
@@ -138,3 +107,9 @@ var postorderTraversal = function(root) {
138107
};
139108

140109
```
110+
111+
## 相关专题
112+
113+
- [二叉树的遍历](https://github.com/azl397985856/leetcode/blob/master/thinkings/binary-tree-traversal.md)
114+
115+

problems/167.two-sum-ii-input-array-is-sorted.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,14 @@ class Solution:
9292
if numbers[left] + numbers[right] == target:
9393
return [left+1, right+1]
9494
```
95+
96+
**复杂度分析**
97+
- 时间复杂度:$O(N)$
98+
- 空间复杂度:$O(1)$
99+
100+
更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经30K star啦。
101+
102+
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
103+
104+
105+
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

problems/26.remove-duplicates-from-sorted-array.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ for (int i = 0; i < len; i++) {
6060

6161
(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)
6262

63+
> 实际上这就是双指针中的快慢指针。在这里快指针是读指针, 慢指针是写指针。
64+
6365
## 关键点解析
6466

6567
- 双指针
@@ -130,3 +132,14 @@ public:
130132
}
131133
};
132134
```
135+
136+
**复杂度分析**
137+
- 时间复杂度:$O(N)$
138+
- 空间复杂度:$O(1)$
139+
140+
更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经30K star啦。
141+
142+
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
143+
144+
145+
![](https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg)

problems/94.binary-tree-inorder-traversal.md

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -66,43 +66,6 @@ Follow up: Recursive solution is trivial, could you do it iteratively?
6666
JavaScript Code:
6767

6868
```js
69-
/*
70-
* @lc app=leetcode id=94 lang=javascript
71-
*
72-
* [94] Binary Tree Inorder Traversal
73-
*
74-
* https://leetcode.com/problems/binary-tree-inorder-traversal/description/
75-
*
76-
* algorithms
77-
* Medium (55.22%)
78-
* Total Accepted: 422.4K
79-
* Total Submissions: 762.1K
80-
* Testcase Example: '[1,null,2,3]'
81-
*
82-
* Given a binary tree, return the inorder traversal of its nodes' values.
83-
*
84-
* Example:
85-
*
86-
*
87-
* Input: [1,null,2,3]
88-
* ⁠ 1
89-
* ⁠ \
90-
* ⁠ 2
91-
* ⁠ /
92-
* ⁠ 3
93-
*
94-
* Output: [1,3,2]
95-
*
96-
* Follow up: Recursive solution is trivial, could you do it iteratively?
97-
*
98-
*/
99-
/**
100-
* Definition for a binary tree node.
101-
* function TreeNode(val) {
102-
* this.val = val;
103-
* this.left = this.right = null;
104-
* }
105-
*/
10669
/**
10770
* @param {TreeNode} root
10871
* @return {number[]}
@@ -277,3 +240,7 @@ class Solution {
277240
}
278241
}
279242
```
243+
244+
## 相关专题
245+
246+
- [二叉树的遍历](https://github.com/azl397985856/leetcode/blob/master/thinkings/binary-tree-traversal.md)

thinkings/binary-tree-traversal.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
## 概述
44

5-
二叉树作为一个基础的数据结构,遍历算法作为一个基础的算法,两者结合当然是经典的组合了。
6-
很多题目都会有 ta 的身影,有直接问二叉树的遍历的,有间接问的。
5+
二叉树作为一个基础的数据结构,遍历算法作为一个基础的算法,两者结合当然是经典的组合了。很多题目都会有 ta 的身影,有直接问二叉树的遍历的,有间接问的。比如要你找到树中满足条件的节点,就是间接考察树的遍历,因为你要找到树中满足条件的点,就需要进行遍历。
76

87
> 你如果掌握了二叉树的遍历,那么也许其他复杂的树对于你来说也并不遥远了
98
@@ -139,8 +138,12 @@ class Solution:
139138
return res
140139
```
141140

141+
可以看出,实现上 WHITE 就表示的是递归中的第一次进入过程,Gray 则表示递归中的从叶子节点返回的过程。 因此这种迭代的写法更接近递归写法的本质。
142+
142143
如要实现前序、后序遍历,只需要调整左右子节点的入栈顺序即可。可以看出使用三色标记法, 其写法类似递归的形式,因此便于记忆和书写,缺点是使用了额外的内存空间。不过这个额外的空间是线性的,影响倒是不大。
143144

145+
> 虽然递归也是额外的线性时间,但是递归的栈开销还是比一个 0,1 变量开销大的。
146+
144147
## Morris 遍历
145148

146149
我们可以使用一种叫做 Morris 遍历的方法,既不使用递归也不借助于栈。从而在$O(1)$时间完成这个过程。
@@ -184,3 +187,15 @@ def MorrisTraversal(root):
184187
```
185188

186189
参考: [what-is-morris-traversal](https://www.educative.io/edpresso/what-is-morris-traversal)
190+
191+
## 相关题目
192+
193+
- [lowest-common-ancestor-of-a-binary-tree](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/)
194+
- [binary-tree-level-order-traversal](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/)
195+
- [binary-tree-zigzag-level-order-traversal](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/)
196+
- [validate-binary-search-tree](https://leetcode-cn.com/problems/validate-binary-search-tree/)
197+
- [maximum-depth-of-binary-tree](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/)
198+
- [balanced-binary-tree](https://leetcode-cn.com/problems/balanced-binary-tree/)
199+
- [binary-tree-level-order-traversal-ii](https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/)
200+
- [binary-tree-maximum-path-sum](https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/)
201+
- [insert-into-a-binary-search-tree](https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/)

thinkings/bit.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,13 @@ class Solution:
190190
- 空间复杂度:$O(1)$
191191

192192

193-
更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经接近30K star啦。
193+
## 相关题目
194194

195-
大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的LeetCode题解
195+
- [number-of-1-bits](https://leetcode-cn.com/problems/number-of-1-bits/)
196+
- [counting-bits](https://leetcode-cn.com/problems/counting-bits/)
197+
- [reverse-bits](https://leetcode-cn.com/problems/reverse-bits/)
196198

197-
![](https://pic.leetcode-cn.com/89ef69abbf02a2957838499a96ce3fbb26830aae52e3ab90392e328c2670cddc-file_1581478989502)
199+
200+
更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经30K star啦。
201+
202+
关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。

thinkings/dynamic-programming.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ f(n) = f(n-1) + f(n-2) 就是【状态转移公式】
184184
185185
## 总结
186186

187-
本篇文章总结了算法中比较常用的两个方法 - 递归和动态规划。
187+
本篇文章总结了算法中比较常用的两个方法 - 递归和动态规划。递归的话可以拿树的题目练手,动态规划的话则将我上面推荐的刷完,再考虑去刷力扣的动态规划标签即可。
188188

189189
如果你只能记住一句话,那么请记住:`递归是从问题的结果倒推,直到问题的规模缩小到寻常。 动态规划是从寻常入手, 逐步扩大规模到最优子结构。`
190190

thinkings/slide-window.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Solution:
7979
return 0 if ans == len(nums) + 1 else ans
8080
```
8181

82-
## 题目列表
82+
## 题目列表(有题解)
8383

8484
以下题目有的信息比较直接,有的题目信息比较隐蔽,需要自己发掘
8585

@@ -94,6 +94,7 @@ class Solution:
9494
- [【1234. 替换子串得到平衡字符串】[Java/C++/Python] Sliding Window](https://leetcode.com/problems/replace-the-substring-for-balanced-string/discuss/408978/javacpython-sliding-window/367697)
9595
- [【1248. 统计「优美子数组」】滑动窗口(Python)](https://leetcode-cn.com/problems/count-number-of-nice-subarrays/solution/1248-tong-ji-you-mei-zi-shu-zu-hua-dong-chuang-kou/)
9696

97+
9798
## 扩展阅读
9899

99100
- [LeetCode Sliding Window Series Discussion](https://leetcode.com/problems/binary-subarrays-with-sum/discuss/186683/)

0 commit comments

Comments
 (0)