Skip to content

Commit 30e1344

Browse files
committed
1
1 parent d65c487 commit 30e1344

File tree

9 files changed

+319
-1
lines changed

9 files changed

+319
-1
lines changed

notes/src/SUMMARY.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,12 @@
7070
- [654. 最大二叉树](./day20/lc654.md)
7171
- [617. 合并二叉树](./day20/lc617.md)
7272
- [700.二叉搜索树中的搜索](./day20/lc700.md)
73-
- [98. 验证二叉搜索树](./day20/lc98.md)
73+
- [98. 验证二叉搜索树](./day20/lc98.md)
74+
- [day 21](./day21.md)
75+
- [530. 二叉搜索树的最小绝对差](./day21/lc530.md)
76+
- [501. 二叉搜索树中的众数](./day21/lc501.md)
77+
- [236. 二叉树的最近公共祖先](./day21/lc236.md)
78+
- [day 22](./day22.md)
79+
- [235. 二叉搜索树的最近公共祖先](./day22/lc235.md)
80+
- [701. 二叉搜索树中的插入操作](./day22/lc701.md)
81+
- [450. 删除二叉搜索树中的节点](./day22/lc450.md)

notes/src/day21.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 第六章 二叉树part07
2+
今日内容
3+
4+
● 530.二叉搜索树的最小绝对差
5+
● 501.二叉搜索树中的众数
6+
● 236. 二叉树的最近公共祖先
7+
8+
详细布置
9+
10+
## 530.二叉搜索树的最小绝对差
11+
12+
需要领悟一下二叉树遍历上双指针操作,优先掌握递归
13+
题目链接/文章讲解:https://programmercarl.com/0530.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E7%BB%9D%E5%AF%B9%E5%B7%AE.html
14+
视频讲解:https://www.bilibili.com/video/BV1DD4y11779
15+
16+
## 501.二叉搜索树中的众数
17+
18+
和 530差不多双指针思路,不过 这里涉及到一个很巧妙的代码技巧。
19+
20+
可以先自己做做看,然后看我的视频讲解。
21+
22+
https://programmercarl.com/0501.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E4%BC%97%E6%95%B0.html
23+
视频讲解:https://www.bilibili.com/video/BV1fD4y117gp
24+
25+
## 236. 二叉树的最近公共祖先
26+
27+
本题其实是比较难的,可以先看我的视频讲解
28+
29+
https://programmercarl.com/0236.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88.html
30+
视频讲解:https://www.bilibili.com/video/BV1jd4y1B7E2

notes/src/day21/lc236.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 236. 二叉树的最近公共祖先
2+
3+
## 题目描述
4+
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
5+
6+
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
7+
8+
## 解题思路
9+
```cpp
10+
class Solution {
11+
public:
12+
TreeNode*pp;
13+
TreeNode*res=0;
14+
TreeNode*qq;
15+
int f(TreeNode*r){
16+
if(!r)return 0;
17+
int left=f(r->left);int right=f(r->right);
18+
int a=r==pp||r==qq?1:0;
19+
a+=left+right;
20+
if(!res&&a==2){res=r;}return a;
21+
}
22+
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
23+
pp=p;qq=q;
24+
f(root);
25+
return res;
26+
}
27+
};
28+
```
29+
30+
f表示在子树中找到的个数,找到2个的时候就设置res就行了
31+
32+
## 学习感想

notes/src/day21/lc501.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 501. 二叉搜索树中的众数
2+
3+
## 题目描述
4+
5+
给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有 众数(即,出现频率最高的元素)。
6+
7+
如果树中有不止一个众数,可以按 任意顺序 返回。
8+
9+
假定 BST 满足如下定义:
10+
11+
结点左子树中所含节点的值 小于等于 当前节点的值
12+
结点右子树中所含节点的值 大于等于 当前节点的值
13+
左子树和右子树都是二叉搜索树
14+
15+
## 解题思路
16+
17+
对我来说有点难了
18+
19+
```cpp
20+
class Solution {
21+
public:
22+
TreeNode*pre=0;
23+
int cnt=0;
24+
int maxcnt=0;
25+
vector<int>res;
26+
void f(TreeNode*p){
27+
if(!p)return;
28+
f(p->left);
29+
if(!pre)cnt=1;else{
30+
if(p->val==pre->val)cnt ++;
31+
else cnt=1;
32+
}
33+
if (cnt>maxcnt){
34+
maxcnt=cnt;res.clear();res.push_back(p->val);
35+
} else if (cnt==maxcnt){
36+
res.push_back(p->val);
37+
}
38+
pre=p;
39+
f(p->right);
40+
}
41+
vector<int> findMode(TreeNode*r) {
42+
res.clear();
43+
f(r);
44+
return res;
45+
}
46+
};
47+
48+
```
49+
## 学习感想

notes/src/day21/lc530.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 530. 二叉搜索树的最小绝对差
2+
3+
## 题目描述
4+
5+
给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。
6+
7+
差值是一个正数,其数值等于两值之差的绝对值。
8+
9+
## 解题思路
10+
11+
一开始没有理解题意写出来的代码,只考虑了一个节点左边和右边,没考虑相隔两层的情况
12+
13+
```cpp
14+
class Solution {
15+
public:
16+
int getMinimumDifference(TreeNode*r) {
17+
if(!r)return 1<<30;
18+
int res = 1<<30;
19+
if(r->left){
20+
int a=r->val-r->left->val;
21+
res=min(res,a);
22+
int b=getMinimumDifference(r->left);
23+
res=min(res,b);
24+
}
25+
if(r->right){
26+
int a=r->right->val-r->val;
27+
res=min(res,a);
28+
int b=getMinimumDifference(r->right);
29+
res=min(res,b);
30+
}
31+
32+
return res;
33+
}
34+
};
35+
```
36+
37+
```cpp
38+
class Solution {
39+
public:
40+
vector<int>v;
41+
void f(TreeNode*r){if(!r)return;f(r->left);v.push_back(r->val);f(r->right);}
42+
int getMinimumDifference(TreeNode*r) {
43+
v.clear();
44+
f(r);
45+
int a=INT_MAX;
46+
for(int i=1;i<v.size();i++)a=min(a,v[i]-v[i-1]);
47+
return a;
48+
}
49+
};
50+
```
51+
遇到在二叉搜索树上求什么最值啊,差值之类的,就把它想成在一个有序数组上求最值,求差值,这样就简单多了。
52+
53+
二叉搜索树顺序遍历:
54+
55+
```cpp
56+
class Solution {
57+
public:
58+
int res = INT_MAX;
59+
TreeNode* pre = NULL;
60+
void f(TreeNode*r){
61+
if(!r)return;
62+
f(r->left);
63+
if(pre){res=min(res,r->val-pre->val);}
64+
pre=r;
65+
f(r->right);
66+
}
67+
int getMinimumDifference(TreeNode*r) {
68+
f(r);
69+
return res;
70+
}
71+
};
72+
```
73+
## 学习感想

notes/src/day22.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 第六章 二叉树part08
2+
今日内容:
3+
4+
● 235. 二叉搜索树的最近公共祖先
5+
● 701.二叉搜索树中的插入操作
6+
● 450.删除二叉搜索树中的节点
7+
8+
详细布置
9+
10+
## 235. 二叉搜索树的最近公共祖先
11+
12+
相对于 二叉树的最近公共祖先 本题就简单一些了,因为 可以利用二叉搜索树的特性。
13+
14+
题目链接/文章讲解:https://programmercarl.com/0235.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E6%9C%80%E8%BF%91%E5%85%AC%E5%85%B1%E7%A5%96%E5%85%88.html
15+
视频讲解:https://www.bilibili.com/video/BV1Zt4y1F7ww
16+
17+
## 701.二叉搜索树中的插入操作
18+
19+
本题比想象中的简单,大家可以先自己想一想应该怎么做,然后看视频讲解,就发现 本题为什么比较简单了。
20+
21+
题目链接/文章讲解:https://programmercarl.com/0701.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E6%8F%92%E5%85%A5%E6%93%8D%E4%BD%9C.html
22+
视频讲解:https://www.bilibili.com/video/BV1Et4y1c78Y
23+
24+
## 450.删除二叉搜索树中的节点
25+
26+
相对于 插入操作,本题就有难度了,涉及到改树的结构
27+
28+
题目链接/文章讲解:https://programmercarl.com/0450.%E5%88%A0%E9%99%A4%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E8%8A%82%E7%82%B9.html
29+
视频讲解:https://www.bilibili.com/video/BV1tP41177us

notes/src/day22/lc235.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 235. 二叉搜索树的最近公共祖先
2+
3+
## 题目描述
4+
5+
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。
6+
7+
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
8+
9+
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]
10+
11+
## 解题思路
12+
13+
当pq同时在两边的话,就是找到了;不是的话,那肯定就是在某一边
14+
15+
```cpp
16+
class Solution {
17+
public:
18+
int mi, ma;
19+
TreeNode*f(TreeNode*r){
20+
if(!r)return 0;
21+
if(mi<=r->val&&r->val<=ma)return r;
22+
if(mi>r->val)return f(r->right);return f(r->left);
23+
}
24+
TreeNode* lowestCommonAncestor(TreeNode* r, TreeNode* p, TreeNode* q) {
25+
mi=min(p->val,q->val);
26+
ma=max(p->val,q->val);
27+
return f(r);
28+
}
29+
};
30+
```
31+
## 学习感想

notes/src/day22/lc450.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 450. 删除二叉搜索树中的节点
2+
3+
## 题目描述
4+
5+
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
6+
7+
一般来说,删除节点可分为两个步骤:
8+
9+
首先找到需要删除的节点;
10+
如果找到了,删除它。
11+
12+
## 解题思路
13+
14+
思路:使用子树代替删除的节点。如果都有的话,随意左还是右。
15+
16+
通用选择右子树,那么子树的子树如何处理?右子树的右子树变成右子树,右子树的左子树变成被删除节点子树上最右侧叶节点的子树。
17+
18+
```cpp
19+
class Solution {
20+
public:
21+
int k;
22+
TreeNode*f(TreeNode* r) {if(!r)return r;
23+
if(r->val==k) {
24+
if(!r->left&&!r->right)return 0;
25+
if(!r->left)return r->right;
26+
if(!r->right)return r->left;
27+
TreeNode*save=r->right->left;
28+
r->right->left=r->left;
29+
r=r->right;TreeNode*p=r->left;
30+
while(p->right)p=p->right;p->right=save;
31+
return r;
32+
}
33+
if (r->val<k)r->right=f(r->right);else r->left=f(r->left);return r;
34+
}
35+
TreeNode* deleteNode(TreeNode* r, int key) {
36+
k=key;r=f(r);return r;
37+
}
38+
};
39+
```
40+
## 学习感想

notes/src/day22/lc701.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 701. 二叉搜索树中的插入操作
2+
3+
## 题目描述
4+
5+
给定二叉搜索树(BST)的根节点 root 和要插入树中的值 value ,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同。
6+
7+
注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果 。
8+
9+
## 解题思路
10+
11+
```cpp
12+
class Solution {
13+
public:
14+
int v;
15+
TreeNode*f(TreeNode*r) {if(!r)return new TreeNode(v);
16+
if(r->val<v)r->right=f(r->right);else r->left=f(r->left);return r;
17+
}
18+
TreeNode* insertIntoBST(TreeNode* r, int val) {
19+
v=val;r=f(r);return r;
20+
}
21+
};
22+
```
23+
24+
WA: 输入的树可能为空树
25+
26+
## 学习感想

0 commit comments

Comments
 (0)