-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
``` | ||
## 学习感想 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
``` | ||
## 学习感想 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
``` | ||
## 学习感想 |