代码随想录算法训练营第二十一天| 669. 修剪二叉搜索树 108.将有序数组转换为二叉搜索树 538.把二叉搜索树转换为累加树

669. 修剪二叉搜索树

1.直接看的视频,讲得非常好
2. 递归一定要想明白终止条件,循环条件和 返回值,别落下了。
class Solution {
public:
    TreeNode* trimBST(TreeNode* root, int low, int high) {
        if(root==NULL) return NULL;
        if(root->val<low)
        {
            return trimBST(root->right,low,high);//这里也要递归,里面可能有符合要求的节点
        }
        if(root->val>high)
        {
            return trimBST(root->left,low,high);
        }
        root->left=trimBST(root->left,low,high);
        root->right=trimBST(root->right,low,high);
        return root;
    }
};

108.将有序数组转换为二叉搜索树

1. 本来以为要分奇偶,后来发现不用,使用递归,每次都分成两半就可以了。需要考虑 nums.size()==0,因为如果数组长度是2的话,分开之后有 一半 的长度是0. 

2. 在分数组的时候,卡了一下。  因为5/2==2 ,不是等于3. 这个是取整符,基本上没有能得到3 的操作。只有取整和取余。  这个地方都卡,有点难评了。

class Solution {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        //使用递归法
        if(nums.size()==1)
        {
            TreeNode* node=new TreeNode(nums[0]);
            return node;
        }
        if(nums.size()==0) return NULL;
        //cout<<nums[nums.size()/2-1];
        TreeNode* root=new TreeNode(nums[nums.size()/2]);
        vector<int> nums1(nums.begin(),nums.begin()+nums.size()/2);
        vector<int> nums2(nums.begin()+nums.size()/2+1,nums.end());
        root->left=sortedArrayToBST(nums1);
        root->right=sortedArrayToBST(nums2);
        return root;
    }
};

538.把二叉搜索树转换为累加树

1. 智障法 先变成数组,再通过数组里面查找index,重新遍历修改二叉树。代码写的越多越容易出错,调用函数的时候参数老出问题老写错

class Solution {
private:
    vector<int> tovector(TreeNode* root)
    {
        vector<int> result;
        traversal(root,result);
        return result;
    }

    void traversal(TreeNode* root, vector<int>& result){
        if(root==NULL) return;     
        traversal(root->left,result);
        result.push_back(root->val);
        traversal(root->right,result);
    }
    int calcSum(vector<int>& nums,int index)
    {
        int sum=0;
        for(int i=index;i<nums.size();i++)
        {
            sum+=nums[i];
        }
        return sum;
    }

    int findIndex(vector<int>& nums,int val)
    {
        for(int i=0;i<nums.size();i++)
        {
            if(nums[i]==val)
            {
                return i;
            }  
        }
        return -1;//需要考虑没找到的情况
    }
public:
    TreeNode* convertBST(TreeNode* root) {
        //遍历两遍,一遍形成数组,另一遍重构 二叉树
        vector<int> a;
        a=tovector(root);
        //重构二叉树
        convertBSThelper(root,a);
        return root;
    }

    void convertBSThelper(TreeNode* root, vector<int>& a)
    {
        if(root==NULL) return;
        convertBSThelper(root->left,a);
        root->val=calcSum(a,findIndex(a,root->val));
        convertBSThelper(root->right,a);
    }
};

2. 逆向思维。对二叉搜索树中序遍历相当于 数组的正序遍历, 因此反中序遍历就相当于数组的 反着遍历。

这里public里面的pre=0,是为了清空之前的pre值。假如一棵二叉树调用过converBST,另一棵二叉树再调用的时候,pre就不正确了

class Solution {
private:
    int pre=0;
    void traversal(TreeNode* cur)
    {
        //右中左遍历
        if(cur==NULL) return;
        traversal(cur->right);
        cur->val+=pre;
        pre=cur->val;
        traversal(cur->left);
    }
public:
    TreeNode* convertBST(TreeNode* root) {
        pre=0;//清空pre的值
        traversal(root);
        return root;
    }
};

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值