LeetCode刷题 Day23

106 阅读2分钟

LeetCode刷题 Day23

669. Trim a Binary Search Tree

Given the root of a binary search tree and the lowest and highest boundaries as low and high, trim the tree so that all its elements lies in [low, high]. Trimming the tree should not change the relative structure of the elements that will remain in the tree (i.e., any node's descendant should remain a descendant). It can be proven that there is a unique answer.

Return the root of the trimmed binary search tree. Note that the root may change depending on the given bounds.

 

Example 1:

Input: root = [1,0,2], low = 1, high = 2
Output: [1,null,2]

Example 2:

Input: root = [3,0,4,null,2,null,null,1], low = 1, high = 3
Output: [3,2,null,1]

思路:

  • 这个题目的重点在于当找到超过范围的root.val的时候如何处理,在遇到超过范围的值时应该往相反的方向继续去递归,因为我们无法判断子树的情况,是需要继续递归去判定

代码:

var trimBST = function(root, low, high) {
    if (!root) 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;
};

时间复杂度: 最差情况 O(N) 空间复杂度: 最差情况 O(N)


108. Convert Sorted Array to Binary Search Tree

Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.

Example 1:

Input: nums = [-10,-3,0,5,9]
Output: [0,-3,9,-10,null,5]
Explanation: [0,-10,5,null,-3,null,9] is also accepted:

Example 2:

Input: nums = [1,3]
Output: [3,1]
Explanation: [1,null,3] and [3,1] are both height-balanced BSTs.

思路:

  • 这道题就是很典型的构造BST,通过不断的分割左右区间来获得左右子树构造BST

代码:

var sortedArrayToBST = function(nums) {
    let left = 0;
    let right = nums.length - 1;
    
    var helper = function(nums, left, right) {
        if (left > right) return null;
        
        let mid = left + Math.floor((right - left) / 2);
        const treeNode = new TreeNode(nums[mid]);
        
        treeNode.left = helper(nums, left, mid - 1);
        treeNode.right = helper(nums, mid + 1, right);
        
        return treeNode;
    }
    
    return helper(nums, left, right);
};

时间复杂度: O(N) 空间复杂度: O(logN) 这个肯定是一个平衡树


538. Convert BST to Greater Tree

Given the root of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.

As a reminder, a binary search tree is a tree that satisfies these constraints:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • Both the left and right subtrees must also be binary search trees.

  Example 1:

Input: root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]

Example 2:

Input: root = [0,null,1]
Output: [1,null,1]

思路:

  • 因为是加上比自己小的数,所以就用BST反向顺序的遍历,同时记录之前的sum

代码:

var convertBST = function(root) {
    let prev = 0;
    var helper = function(root) {
        if (!root) return 0;
    
        helper(root.right);
        root.val = root.val + prev;
        prev = root.val;
        helper(root.left);
    }
    
    helper(root);
    return root;
};

时间复杂度: O(N) 空间复杂度: O(N)