Skip to content

Commit 15178d4

Browse files
committed
1
1 parent 30e1344 commit 15178d4

File tree

5 files changed

+89
-1
lines changed

5 files changed

+89
-1
lines changed

notes/src/SUMMARY.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,8 @@
7878
- [day 22](./day22.md)
7979
- [235. 二叉搜索树的最近公共祖先](./day22/lc235.md)
8080
- [701. 二叉搜索树中的插入操作](./day22/lc701.md)
81-
- [450. 删除二叉搜索树中的节点](./day22/lc450.md)
81+
- [450. 删除二叉搜索树中的节点](./day22/lc450.md)
82+
- [day 23](./day23.md)
83+
- [669. 修剪二叉搜索树](./day23/lc669.md)
84+
- [108. 将有序数组转换为二叉搜索树](./day23/lc108.md)
85+
- [538. 把二叉搜索树转换为累加树](./day23/lc538.md)

notes/src/day23.md

Whitespace-only changes.

notes/src/day23/lc108.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 108. 将有序数组转换为二叉搜索树
2+
## 题目描述
3+
4+
给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。
5+
6+
高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。
7+
8+
## 解题思路
9+
10+
```cpp
11+
class Solution {
12+
public:
13+
vector<int>v;
14+
TreeNode*f(int l,int r){
15+
int n=r-l;if(n<=0)return 0;
16+
int m=l+n/2;return new TreeNode(v[m],f(l,m),f(m+1,r));
17+
}
18+
TreeNode* sortedArrayToBST(vector<int>&ve) {
19+
v=ve;int n=v.size();
20+
return f(0,n);
21+
}
22+
};
23+
```
24+
25+
## 学习感想

notes/src/day23/lc538.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 538. 把二叉搜索树转换为累加树
2+
## 题目描述
3+
4+
给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。
5+
6+
提醒一下,二叉搜索树满足下列约束条件:
7+
8+
节点的左子树仅包含键 小于 节点键的节点。
9+
节点的右子树仅包含键 大于 节点键的节点。
10+
左右子树也必须是二叉搜索树。
11+
注意:本题和 1038: https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/ 相同
12+
13+
## 解题思路
14+
15+
```cpp
16+
class Solution {
17+
public:
18+
int s=0;
19+
TreeNode* convertBST(TreeNode*r) {
20+
if(!r)return r;
21+
convertBST(r->right);
22+
s+=r->val;
23+
r->val=s;
24+
convertBST(r->left);
25+
return r;
26+
}
27+
};
28+
```
29+
## 学习感想

notes/src/day23/lc669.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 669. 修剪二叉搜索树
2+
3+
## 题目描述
4+
5+
给你二叉搜索树的根节点 root ,同时给定最小边界low 和最大边界 high。通过修剪二叉搜索树,使得所有节点的值在[low, high]中。修剪树 不应该 改变保留在树中的元素的相对结构 (即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在 唯一的答案 。
6+
7+
所以结果应当返回修剪好的二叉搜索树的新的根节点。注意,根节点可能会根据给定的边界发生改变。
8+
9+
10+
## 解题思路
11+
12+
```cpp
13+
class Solution {
14+
public:
15+
int l, ri;
16+
TreeNode*f(TreeNode*r){
17+
if(!r)return r;
18+
if(r->val>=l&&r->val<=ri) {
19+
r->left=f(r->left);
20+
r->right=f(r->right);
21+
return r;
22+
}
23+
if(r->val<l)return f(r->right);return f(r->left);
24+
}
25+
TreeNode* trimBST(TreeNode* r, int low, int high) {
26+
l=low;ri=high;r=f(r);return r;
27+
}
28+
};
29+
```
30+
## 学习感想

0 commit comments

Comments
 (0)