Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a64f7a1

Browse files
Update
1 parent 697b8ac commit a64f7a1

12 files changed

+124
-99
lines changed

‎problems/0098.验证二叉搜索树.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ public:
8787
8888
写出了类似这样的代码:
8989
90-
```
90+
```CPP
9191
if (root->val > root->left->val && root->val < root->right->val) {
9292
return true;
9393
} else {
9494
return false;
9595
}
9696
```
9797

98-
**我们要比较的是 左子树所有节点小于中间节点,右子树所有节点大于中间节点。**所以以上代码的判断逻辑是错误的。
98+
**我们要比较的是 左子树所有节点小于中间节点,右子树所有节点大于中间节点**所以以上代码的判断逻辑是错误的。
9999

100100
例如: [10,5,15,null,null,6,20] 这个case:
101101

@@ -125,7 +125,7 @@ if (root->val > root->left->val && root->val < root->right->val) {
125125

126126
代码如下:
127127

128-
```
128+
```CPP
129129
long long maxVal = LONG_MIN; // 因为后台测试数据中有int最小值
130130
bool isValidBST(TreeNode* root)
131131
```
@@ -138,7 +138,7 @@ bool isValidBST(TreeNode* root)
138138
139139
代码如下:
140140
141-
```
141+
```CPP
142142
if (root == NULL) return true;
143143
```
144144

@@ -148,7 +148,7 @@ if (root == NULL) return true;
148148

149149
代码如下:
150150

151-
```
151+
```CPP
152152
bool left = isValidBST(root->left); //
153153

154154
// 中序遍历,验证遍历的元素是不是从小到大

‎problems/0104.二叉树的最大深度.md‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
66

77

8-
看完本篇可以一起做了如下两道题目:
9-
10-
* 104.二叉树的最大深度
11-
* 559.n叉树的最大深度
12-
138
# 104.二叉树的最大深度
149

1510
[力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/)
@@ -27,6 +22,16 @@
2722

2823
返回它的最大深度 3 。
2924

25+
# 思路
26+
27+
看完本篇可以一起做了如下两道题目:
28+
29+
* 104.二叉树的最大深度
30+
* 559.n叉树的最大深度
31+
32+
《代码随想录》算法视频公开课:[二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度](https://www.bilibili.com/video/BV1Gd4y1V75u),相信结合视频在看本篇题解,更有助于大家对本题的理解。
33+
34+
3035
## 递归法
3136

3237
本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。

‎problems/0110.平衡二叉树.md‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131

3232
返回 false 。
3333

34-
# 题外话
3534

35+
**《代码随想录》算法视频公开课:[后序遍历求高度,高度判断是否平衡 | LeetCode:110.平衡二叉树](https://www.bilibili.com/video/BV1Ug411S7my),相信结合视频在看本篇题解,更有助于大家对本题的理解**
36+
37+
## 题外话
3638

3739
咋眼一看这道题目和[104.二叉树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html)很像,其实有很大区别。
3840

@@ -113,9 +115,9 @@ public:
113115
};
114116
```
115117
116-
# 本题思路
118+
## 本题思路
117119
118-
## 递归
120+
### 递归
119121
120122
此时大家应该明白了既然要求比较高度,必然是要后序遍历。
121123
@@ -225,7 +227,7 @@ public:
225227
};
226228
```
227229

228-
## 迭代
230+
### 迭代
229231

230232
[104.二叉树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html)中我们可以使用层序遍历来求深度,但是就不能直接用层序遍历来求高度了,这就体现出求高度和求深度的不同。
231233

@@ -342,7 +344,7 @@ public:
342344
343345
因为对于回溯算法已经是非常复杂的递归了,如果在用迭代的话,就是自己给自己找麻烦,效率也并不一定高。
344346
345-
# 总结
347+
## 总结
346348
347349
通过本题可以了解求二叉树深度 和 二叉树高度的差异,求深度适合用前序遍历,而求高度适合用后序遍历。
348350
@@ -351,9 +353,9 @@ public:
351353
但是递归方式是一定要掌握的!
352354
353355
354-
# 其他语言版本
356+
## 其他语言版本
355357
356-
## Java
358+
### Java
357359
358360
```Java
359361
class Solution {
@@ -494,7 +496,7 @@ class Solution {
494496
}
495497
```
496498

497-
## Python
499+
### Python
498500

499501
递归法:
500502
```python
@@ -554,7 +556,7 @@ class Solution:
554556
```
555557

556558

557-
## Go
559+
### Go
558560
```Go
559561
func isBalanced(root *TreeNode) bool {
560562
if root==nil{
@@ -590,7 +592,7 @@ func abs(a int)int{
590592
}
591593
```
592594

593-
## JavaScript
595+
### JavaScript
594596
递归法:
595597
```javascript
596598
var isBalanced = function(root) {
@@ -658,7 +660,7 @@ var isBalanced = function (root) {
658660
};
659661
```
660662

661-
## TypeScript
663+
### TypeScript
662664

663665
```typescript
664666
// 递归法
@@ -676,7 +678,7 @@ function isBalanced(root: TreeNode | null): boolean {
676678
};
677679
```
678680

679-
## C
681+
### C
680682

681683
递归法:
682684
```c
@@ -780,7 +782,7 @@ bool isBalanced(struct TreeNode* root){
780782
}
781783
```
782784

783-
## Swift:
785+
### Swift:
784786

785787
>递归
786788
```swift

‎problems/0111.二叉树的最小深度.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727

2828
# 思路
2929

30+
《代码随想录》算法视频公开课:[看起来好像做过,一写就错! | LeetCode:111.二叉树的最小深度](https://www.bilibili.com/video/BV1QD4y1B7e2),相信结合视频在看本篇题解,更有助于大家对本题的理解。
31+
32+
3033
看完了这篇[104.二叉树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html),再来看看如何求最小深度。
3134

3235
直觉上好像和求最大深度差不多,其实还是差不少的。

‎problems/0112.路径总和.md‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ return false;
123123
整体代码如下:
124124

125125
```cpp
126-
class solution {
126+
class Solution {
127127
private:
128-
bool traversal(treenode* cur, int count) {
128+
bool traversal(TreeNode* cur, int count) {
129129
if (!cur->left && !cur->right && count == 0) return true; // 遇到叶子节点,并且计数为0
130130
if (!cur->left && !cur->right) return false; // 遇到叶子节点直接返回
131131

@@ -143,8 +143,8 @@ private:
143143
}
144144

145145
public:
146-
bool haspathsum(treenode* root, int sum) {
147-
if (root == null) return false;
146+
bool hasPathSum(TreeNode* root, int sum) {
147+
if (root == NULL) return false;
148148
return traversal(root, sum - root->val);
149149
}
150150
};
@@ -155,7 +155,7 @@ public:
155155
```cpp
156156
class solution {
157157
public:
158-
bool haspathsum(treenode* root, int sum) {
158+
bool hasPathSum(TreeNode* root, int sum) {
159159
if (root == null) return false;
160160
if (!root->left && !root->right && sum == root->val) {
161161
return true;
@@ -176,7 +176,7 @@ public:
176176
177177
c++就我们用pair结构来存放这个栈里的元素。
178178
179-
定义为:`pair<treenode*, int>` pair<节点指针,路径数值>
179+
定义为:`pair<TreeNode*, int>` pair<节点指针,路径数值>
180180
181181
这个为栈里的一个元素。
182182
@@ -186,25 +186,25 @@ c++就我们用pair结构来存放这个栈里的元素。
186186
class solution {
187187
188188
public:
189-
bool haspathsum(treenode* root, int sum) {
189+
bool haspathsum(TreeNode* root, int sum) {
190190
if (root == null) return false;
191191
// 此时栈里要放的是pair<节点指针,路径数值>
192-
stack<pair<treenode*, int>> st;
193-
st.push(pair<treenode*, int>(root, root->val));
192+
stack<pair<TreeNode*, int>> st;
193+
st.push(pair<TreeNode*, int>(root, root->val));
194194
while (!st.empty()) {
195-
pair<treenode*, int> node = st.top();
195+
pair<TreeNode*, int> node = st.top();
196196
st.pop();
197197
// 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true
198198
if (!node.first->left && !node.first->right && sum == node.second) return true;
199199
200200
// 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来
201201
if (node.first->right) {
202-
st.push(pair<treenode*, int>(node.first->right, node.second + node.first->right->val));
202+
st.push(pair<TreeNode*, int>(node.first->right, node.second + node.first->right->val));
203203
}
204204
205205
// 左节点,压进去一个节点的时候,将该节点的路径数值也记录下来
206206
if (node.first->left) {
207-
st.push(pair<treenode*, int>(node.first->left, node.second + node.first->left->val));
207+
st.push(pair<TreeNode*, int>(node.first->left, node.second + node.first->left->val));
208208
}
209209
}
210210
return false;

‎problems/0222.完全二叉树的节点个数.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
# 思路
3434

35+
《代码随想录》算法视频公开课:[要理解普通二叉树和完全二叉树的区别! | LeetCode:222.完全二叉树节点的数量](https://www.bilibili.com/video/BV1eW4y1B7pD),相信结合视频在看本篇题解,更有助于大家对本题的理解。
36+
3537
本篇给出按照普通二叉树的求法以及利用完全二叉树性质的求法。
3638

3739
## 普通二叉树

‎problems/0257.二叉树的所有路径.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
# 思路
2222

23+
**《代码随想录》算法视频公开课:[递归中带着回溯,你感受到了没?| LeetCode:257. 二叉树的所有路径](https://www.bilibili.com/video/BV1ZG411G7Dh),相信结合视频在看本篇题解,更有助于大家对本题的理解**
24+
2325
这道题目要求从根节点到叶子的路径,所以需要前序遍历,这样才方便让父节点指向孩子节点,找到对应的路径。
2426

2527
在这道题目中将第一次涉及到回溯,因为我们要把路径记录下来,需要回溯来回退一一个路径在进入另一个路径。

‎problems/0404.左叶子之和.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
# 思路
1919

20+
**《代码随想录》算法视频公开课:[二叉树的题目中,总有一些规则让你找不到北 | LeetCode:404.左叶子之和](https://www.bilibili.com/video/BV1GY4y1K7z8),相信结合视频在看本篇题解,更有助于大家对本题的理解**
21+
22+
2023
**首先要注意是判断左叶子,不是二叉树左侧节点,所以不要上来想着层序遍历。**
2124

2225
因为题目中其实没有说清楚左叶子究竟是什么节点,那么我来给出左叶子的明确定义:**节点A的左孩子不为空,且左孩子的左右孩子都为空(说明是叶子节点),那么A节点的左孩子为左叶子节点**

‎problems/0501.二叉搜索树中的众数.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void searchBST(TreeNode* cur, unordered_map<int, int>& map) { // 前序遍历
7272
7373
代码如下:
7474
75-
```
75+
```CPP
7676
bool static cmp (const pair<int, int>& a, const pair<int, int>& b) {
7777
return a.second > b.second; // 按照频率从大到小排序
7878
}
@@ -169,7 +169,7 @@ void searchBST(TreeNode* cur) {
169169

170170
代码如下:
171171

172-
```
172+
```CPP
173173
if (pre == NULL) { // 第一个节点
174174
count = 1; // 频率为1
175175
} else if (pre->val == cur->val) { // 与前一个节点数值相同
@@ -194,7 +194,7 @@ pre = cur; // 更新上一个节点
194194

195195
如果 频率count 等于 maxCount(最大频率),当然要把这个元素加入到结果集中(以下代码为result数组),代码如下:
196196

197-
```
197+
```CPP
198198
if (count == maxCount) { // 如果和最大值相同,放进result中
199199
result.push_back(cur->val);
200200
}
@@ -206,7 +206,7 @@ if (count == maxCount) { // 如果和最大值相同,放进result中
206206

207207
频率count 大于 maxCount的时候,不仅要更新maxCount,而且要清空结果集(以下代码为result数组),因为结果集之前的元素都失效了。
208208

209-
```
209+
```CPP
210210
if (count > maxCount) { // 如果计数大于最大值
211211
maxCount = count; // 更新最大频率
212212
result.clear(); // 很关键的一步,不要忘记清空result,之前result里的元素都失效了

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /