代码随想录训练营day23|树:669.修剪二叉树、108.将有序数组转化为二叉搜索树、538.把二叉搜索树转换为累加树

LeetCode669修剪二叉树

思路:

  1. 终止条件:node == nullptr 此时直接返回node。因为,这题目不要求一句子节点来判断。
  2. 单层处理逻辑:
    1. 如果遇到了小于low的节点,就继续寻找这个节点的右子节点;同理遇到了大于high的节点就继续寻找这个节点的左子节点。
    if(root->val > high){
            TreeNode* left = trimBST(root->left, low, high);
            return left;
        }
        if(root->val < low){
            TreeNode* right = trimBST(root->right, low, high);
            return right;
        }
    
    1. 如果相等:这个节点是要求保留的,因此继续出来当前节点的左右子树。
        root->left = trimBST(root->left, low,high);
        root->right = trimBST(root->right, low, high);
    
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode trimBST(TreeNode root, int low, int high) {
        // 在处理单层逻辑的时候如何将不在区间的节点删除比较绕
        // 其实就是在相应的情况下,把要删除的左/右子树传递给返回给root的上一个节点
        if(root == null){
            return null;
        }
        // 单层递归逻辑
        if(root.val < low){ // 如果出现了小于low的root,就他的右子树的结果返回给上一节点
        TreeNode right= trimBST(root.right, low, high);
        return right; // 这一步就相当于把root删除了,因为当root入栈之后返回的结果是root.right
        // 相较于之前的递归,这里多了一个return,可以实现删除当前root的功能
        }
        else 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;
    }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {

public:
    TreeNode* trimBST(TreeNode* root, int low, int high) {
        if(root == nullptr) return root;

        if(root->val > high){
            TreeNode* left = trimBST(root->left, low, high);
            return left;
        }
        if(root->val < low){
            TreeNode* right = trimBST(root->right, low, high);
            return right;
        }
        root->left = trimBST(root->left, low,high);
        root->right = trimBST(root->right, low, high);
        return root;
    }
};

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

思路:因为原数组已经有序了,所以可以先找到mid (mid一定就是父节点)。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        // 对于数组问题,就要想到左闭右闭
        return toBST(nums, 0, nums.length-1);
    }
    public TreeNode toBST(int[] nums, int left, int right){
        if(right< left){
            return null;
        }
        // 单层逻辑
        if(right-left == 0){
            return new TreeNode(nums[left]);
        }
        int mid = left + (right - left) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = toBST(nums, left, mid -1);
        root.right = toBST(nums, mid+1, right);
        return root;
    }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    TreeNode* getTree(vector<int>& nums, int left, int right){
        if(left > right) return NULL;
        if(left == right){
            return new TreeNode(nums[left]);
        }
        int mid = left + ((right - left) >> 1);
        TreeNode* node = new TreeNode(nums[mid]);
        node->left = getTree(nums, left, mid-1);
        node->right = getTree(nums,mid+1, right);
        return node;
    }
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        int right = nums.size() - 1;
        int left = 0;
        return getTree(nums, left, right);
    }
};

LeetCode538:把二叉搜索树转换为累加树

思路:这个题目要实现的就是从最右子节点开始逐步累加,按照右中左的顺序。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int sum;
    public TreeNode convertBST(TreeNode root) {
        sum(root);
        return root;
    }
    public void sum(TreeNode root){
        if(root == null){
            return;
        }
        // 右中左 逆中序
        sum(root.right); // 右
        sum += root.val; // 累加
        root.val = sum;  // sum赋值给当前的root
        sum(root.left);
    }
}

别忘了二叉树的遍历,遍历递归的第一步处理的是终止条件,然后就会依次处理叶子结点,一层层网上走了。由于是二叉搜索树,只要右中左的顺序就可以得到结果了。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
private:
    int ans = 0;
public:
    TreeNode* bstToGst(TreeNode* root) {
        if(root == nullptr )return 0;
        bstToGst(root->right);
        ans += root->val;
        root->val = ans;
        bstToGst(root->left);
        return root;
    }
};

你可能感兴趣的:(LeetCode刷题,leetcode,算法,数据结构)