美文网首页
Binary search tree

Binary search tree

作者: MrWheat | 来源:发表于2018-09-02 23:26 被阅读0次

    Contains Duplicate III

    Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
    Example 1:
    Input: nums = [1,2,3,1], k = 3, t = 0
    Output: true
    Example 2:
    Input: nums = [1,0,1,1], k = 1, t = 2
    Output: true
    Example 3:
    Input: nums = [1,5,9,1,5,9], k = 2, t = 3
    Output: false

    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        TreeSet<Integer> set = new TreeSet<>();
        for (int i = 0; i < nums.length; ++i) {
            // Find the successor of current element
            Integer s = set.ceiling(nums[i]);
            if (s != null && s <= nums[i] + t) return true;
    
            // Find the predecessor of current element
            Integer g = set.floor(nums[i]);
            if (g != null && nums[i] <= g + t) return true;
    
            set.add(nums[i]);
            if (set.size() > k) {
                set.remove(nums[i - k]);
            }
        }
        return false;
    }
    

    注意:java数据集TreeSet,建立一棵二叉搜索树,需要找一个数最相近的两树的时候,可以用Binary search tree

    683. K Empty Slots

    530. Minimum Absolute Difference in BST

    Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.
    Example:
    Input:
    1

    3
    /
    2
    Output:
    1
    Explanation:
    The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).

    The most common idea is to first inOrder traverse the tree and compare the delta between each of the adjacent values. It's guaranteed to have the correct answer because it is a BST thus inOrder traversal values are sorted.
    
    Solution 1 - In-Order traverse, time complexity O(N), space complexity O(1).
    
    public class Solution {
        int min = Integer.MAX_VALUE;
        Integer prev = null;
        
        public int getMinimumDifference(TreeNode root) {
            if (root == null) return min;
            
            getMinimumDifference(root.left);
            
            if (prev != null) {
                min = Math.min(min, root.val - prev);
            }
            prev = root.val;
            
            getMinimumDifference(root.right);
            
            return min;
        }
        
    }
    What if it is not a BST? (Follow up of the problem) The idea is to put values in a TreeSet and then every time we can use O(lgN) time to lookup for the nearest values.
    
    Solution 2 - Pre-Order traverse, time complexity O(NlgN), space complexity O(N).
    
    public class Solution {
        TreeSet<Integer> set = new TreeSet<>();
        int min = Integer.MAX_VALUE;
        
        public int getMinimumDifference(TreeNode root) {
            if (root == null) return min;
            
            if (!set.isEmpty()) {
                if (set.floor(root.val) != null) {
                    min = Math.min(min, root.val - set.floor(root.val));
                }
                if (set.ceiling(root.val) != null) {
                    min = Math.min(min, set.ceiling(root.val) - root.val);
                }
            }
            
            set.add(root.val);
            
            getMinimumDifference(root.left);
            getMinimumDifference(root.right);
            
            return min;
        }
    }
    

    相关文章

      网友评论

          本文标题:Binary search tree

          本文链接:https://www.haomeiwen.com/subject/gspvwftx.html