Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
Ainevsia committed Aug 2, 2023
1 parent 30e1344 commit 15178d4
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
6 changes: 5 additions & 1 deletion notes/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@
- [day 22](./day22.md)
- [235. 二叉搜索树的最近公共祖先](./day22/lc235.md)
- [701. 二叉搜索树中的插入操作](./day22/lc701.md)
- [450. 删除二叉搜索树中的节点](./day22/lc450.md)
- [450. 删除二叉搜索树中的节点](./day22/lc450.md)
- [day 23](./day23.md)
- [669. 修剪二叉搜索树](./day23/lc669.md)
- [108. 将有序数组转换为二叉搜索树](./day23/lc108.md)
- [538. 把二叉搜索树转换为累加树](./day23/lc538.md)
Empty file added notes/src/day23.md
Empty file.
25 changes: 25 additions & 0 deletions notes/src/day23/lc108.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 108. 将有序数组转换为二叉搜索树
## 题目描述

给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。

高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。

## 解题思路

```cpp
class Solution {
public:
vector<int>v;
TreeNode*f(int l,int r){
int n=r-l;if(n<=0)return 0;
int m=l+n/2;return new TreeNode(v[m],f(l,m),f(m+1,r));
}
TreeNode* sortedArrayToBST(vector<int>&ve) {
v=ve;int n=v.size();
return f(0,n);
}
};
```
## 学习感想
29 changes: 29 additions & 0 deletions notes/src/day23/lc538.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 538. 把二叉搜索树转换为累加树
## 题目描述

给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。

提醒一下,二叉搜索树满足下列约束条件:

节点的左子树仅包含键 小于 节点键的节点。
节点的右子树仅包含键 大于 节点键的节点。
左右子树也必须是二叉搜索树。
注意:本题和 1038: https://leetcode-cn.com/problems/binary-search-tree-to-greater-sum-tree/ 相同

## 解题思路

```cpp
class Solution {
public:
int s=0;
TreeNode* convertBST(TreeNode*r) {
if(!r)return r;
convertBST(r->right);
s+=r->val;
r->val=s;
convertBST(r->left);
return r;
}
};
```
## 学习感想
30 changes: 30 additions & 0 deletions notes/src/day23/lc669.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 669. 修剪二叉搜索树

## 题目描述

给你二叉搜索树的根节点 root ,同时给定最小边界low 和最大边界 high。通过修剪二叉搜索树,使得所有节点的值在[low, high]中。修剪树 不应该 改变保留在树中的元素的相对结构 (即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在 唯一的答案 。

所以结果应当返回修剪好的二叉搜索树的新的根节点。注意,根节点可能会根据给定的边界发生改变。


## 解题思路

```cpp
class Solution {
public:
int l, ri;
TreeNode*f(TreeNode*r){
if(!r)return r;
if(r->val>=l&&r->val<=ri) {
r->left=f(r->left);
r->right=f(r->right);
return r;
}
if(r->val<l)return f(r->right);return f(r->left);
}
TreeNode* trimBST(TreeNode* r, int low, int high) {
l=low;ri=high;r=f(r);return r;
}
};
```
## 学习感想

0 comments on commit 15178d4

Please sign in to comment.