Day23
669. 修剪二叉搜索树
递归法
用法也是如上,使用返回值来确定父子节点的关系(我认为在需要遍历树,并且要确定哪个节点需要保留,也可以说是需要删除一些节点的情况比较好用)。
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(root==nullptr) return nullptr;
if(root->val < low){
TreeNode* right=trimBST(root->right,low,high);
return right;
}
if(root->val > high){
TreeNode* left=trimBST(root->left,low,high);
return left;
}
root->left=trimBST(root->left,low,high);
root->right=trimBST(root->right,low,high);
return root;
}
};
迭代法
首先对于搜索树来说,迭代法不需要使用栈
其次本题需要考虑根节点的问题,后面的while一开始处理的就是根节点的左右孩子节点,所以要保证根节点的数值在low和high之间。
这里面while循环的判定条件需要仔细考虑
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(!root) return nullptr;
while(root!=nullptr&&(root->val>high ||root->val<low)){
if(root->val>high){
root=root->left;
}
else{
root=root->right;
}
}
TreeNode* cur=root;
while(cur!=nullptr){
while(cur->left && cur->left->val < low){
cur->left=cur->left->right;
}
cur=cur->left;
}
cur=root;
while(cur!=nullptr){
while(cur->right&&cur->right->val>high){
cur->right=cur->right->left;
}
cur=cur->right;
}
return root;
}
};
108.将有序数组转换为二叉搜索树
递归法:
也是使用返回值来确定父子节点关系,搜索树构造的时候只需要是找到mid节点然后左右递归就可以了。保证是中间数组的值做根节点可以保证左右两侧的深度误差不超过1,这也是递归的一个思路,也就是从一个小的循环找到规律。
class Solution {
public:
TreeNode* travelsal(vector<int>& nums,int left,int right){
if(left>right) return nullptr;
int mid=(left+right)/2;
TreeNode* cur=new TreeNode(nums[mid]);
cur->left=travelsal(nums,left,mid-1);
cur->right=travelsal(nums,mid+1,right);
return cur;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
TreeNode* root=travelsal(nums,0,nums.size()-1);
return root;
}
};
迭代法:
关于小循环里面定义的局部变量,放在栈或者队列中,是将指针副本放入栈中存储,所以局部变量释放后,Treenode对象没有被释放,同时指针副本仍然存在,还是可以找到这个对象。
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
if(nums.size()==0) return nullptr;
TreeNode* root=new TreeNode(0);
queue<TreeNode*> nodeque;
queue<int> leftque;
queue<int> rightque;
nodeque.push(root);
leftque.push(0);
rightque.push(nums.size()-1);
while(!nodeque.empty()){
TreeNode* cur=nodeque.front();nodeque.pop();
int left=leftque.front(); leftque.pop();
int right=rightque.front(); rightque.pop();
int mid=left+(right-left)/2;
cur->val=nums[mid];
if(left<=mid-1){
TreeNode* node=new TreeNode(0);
cur->left=node;
nodeque.push(node);
leftque.push(left);
rightque.push(mid-1);
}
if(right>=mid+1){
TreeNode* node=new TreeNode(0);
cur->right=node;
nodeque.push(node);
leftque.push(mid+1);
rightque.push(right);
}
}
return root;
}
};
538.把二叉搜索树转换为累加树
首先明白什么是累加树,其实就是把大于等于该节点的所有值加起来放在这个节点,
所以可以从最大的数开始,将该节点的值依次加上之前的累加和放在该节点,对于搜索树,只需要倒序遍历就可以了,使用反向中序遍历递归处理。
class Solution {
public:
int pre=0;
void travelsal(TreeNode* cur){
if(cur==nullptr) return;
travelsal(cur->right);
cur->val+=pre;
pre=cur->val;
travelsal(cur->left);
return;
}
TreeNode* convertBST(TreeNode* root) {
if(root==nullptr) return root;
travelsal(root);
return root;
}
};
迭代法:
class Solution {
public:
int pre=0;
void travelsal(TreeNode* root){
stack<TreeNode*> st;
TreeNode* cur=root;
while(cur!=nullptr||!st.empty()){
if(cur!=nullptr){
st.push(cur);
cur=cur->right;
}
else{
cur=st.top();
st.pop();
cur->val+=pre;
pre=cur->val;
cur=cur->left;
}
}
}
TreeNode* convertBST(TreeNode* root) {
pre=0;
travelsal(root);
return root;
}
};