CS
Algorithm
Algorithm
  • lintcode
  • EPI
    • String
      • Base Convert
  • Graph
    • Inorder Successor in BST
    • Balanced Binary Tree
    • All Paths From Source to Target
  • LinkedIn
    • House robber II
    • Single Number
    • Flatten Binary Tree to Linked List
    • Smallest Difference pair of values between two unsorted Arrays
    • Word Search II
    • Implement Trie (Prefix Tree)
    • K Closest Points
    • implement BST
    • HashMap
    • Implement strStr()
    • Min Stack
    • Meeting Rooms
    • Shortest Completing Word
    • Longest Palindromic Substring
    • Longest Palindromic Subsequence
    • Count Different Palindromic Subsequences
    • Palindromic Substrings
    • Sparse Matrix Multiplication
    • Insert Delete GetRandom O(1) - Duplicates allowed
    • Bulb Switcher
    • Verify Preorder Sequence in Binary Search Tree
    • Untitled
    • Find the Celebrity
    • Coin Change
    • Partition Equal Subset Sum
    • Permutation Sequence
    • Next Permutation
    • Kth Smallest Element in a BST
    • Word Search
    • Word Break
    • Shuffle an Array
    • Add Two Numbers
    • Longest Substring Without Repeating Characters
    • Longest Increasing Subsequence
    • 3Sum Smaller
    • LFU Cache
    • Copy List with Random Pointer
    • Linked List Cycle
    • Merge Sorted Array
    • Two Sum II - Input array is sorted
    • Search Insert Position
    • Find First and Last Position of Element in Sorted Array
    • Combination Sum
    • Path Sum
    • Roman to Integer
    • Valid Parentheses
    • Product of Array Except Self
    • Permutations
    • 3Sum
    • Reverse Integer
    • Longest Common Subsequence and substring
    • Implement Stack using Queues
    • Sort Characters By Frequency
    • Delete Node in a BST
    • Invert Binary Tree
    • Serialize and Deserialize BST
    • Reverse String
    • Binary Tree Zigzag Level Order Traversal
    • Friend Circles
    • Letter Combinations of a Phone Number
    • Fizz Buzz
    • Encode and Decode TinyURL
    • Binary Tree Right Side View
    • Shortest Word Distance III
    • Binary Search Tree Iterator
    • Kth Largest Element in an Array
    • Clone Graph
    • Lowest Common Ancestor III
    • Lowest Common Ancestor II
    • Reverse Words in a String
    • Path Sum
    • Find K Pairs with Smallest Sums
    • Validate Binary Search Tree
    • All O`one Data Structure
    • Top K Frequent Elements
    • Integer to Roman
    • Shortest Word Distance III
    • Edit Distance
    • Profitable Schemes
    • Minimum Window Substring
    • LRU Cache
    • Text Justification
    • Integer to English Words
    • Partition to K Equal Sum Subsets
    • Graph Valid Tree
    • Exclusive Time of Functions
    • Repeated DNA Sequences
    • Valid Number
    • Insert Delete GetRandom O(1)
    • Same Tree
    • Friends Within Three Jumps
    • Isomorphic Strings
    • Sum of Square Numbers
    • Valid Perfect Square
    • Evaluate Reverse Polish Notation
    • House Robber
    • Palindromic Substrings
    • Find Largest Value in Each Tree Row
    • Can Place Flowers
    • Insert Interval
    • Maximum Depth of Binary Tree
    • Two Sum
    • Paint House II
    • Max Points on a Line
    • Word Ladder II
    • Word Ladder
    • Validate IP Address
    • Maximum Product Subarray
    • Factor Combinations
    • Flatten Nested List Iterator
    • Max Stack
    • Number of Connected Components in an Undirected Graph
    • Combination Sum II
    • Permutations II
    • Permutations
    • Climbing Stairs
    • Paint House
    • Closest Binary Search Tree Value
    • Closest Binary Search Tree Value II
    • Rotate String
    • Max Area of Island
    • Maximum Subarray
    • Serialize and Deserialize Binary Tree
    • Second Minimum Node In a Binary Tree
    • Lowest Common Ancestor of a Binary Tree
    • Lowest Common Ancestor of a Binary Search Tree
    • Symmetric Tree
    • Binary Tree Upside Down
    • Maximum Depth of Binary Tree
    • Find Leaves of Binary Tree
    • Number of Islands
    • Nested List Weight Sum II
    • Nested List Weight Sum
    • Merge Intervals
    • Valid Triangle Number
    • Find K Closest Elements
    • Find Smallest Letter Greater Than Target
    • Pow(x,n)
    • Search in Rotated Sorted Array II
    • Search in Rotated Sorted Array
    • Sqrt(x)
    • Intersection of Two Linked Lists
    • Binary Tree Level Order Traversal
    • Shortest Word Distance
    • Two Sum III - Data structure design
    • Shortest Word Distance II
  • Binary Search
    • Find K Closest Elements
    • Find Min In Rotated Sorted Array
    • Find Peak Element
    • First Bad Version
    • First Position Of Target
    • Guess Num Higer Or Lower
    • Last Position Of Target
    • Longest Increasing Subsequence
    • Russian Doll Envelopes
    • Search In Big Sorted Array
    • Search Insert Position
    • Single Number IV
    • pow(x,n)
    • sqrt
    • Search in Rotated Sorted Array
    • Search in Rotated Sorted Array II
    • Find Minimum in Rotated Sorted Array
    • Find Minimum in Rotated Sorted Array II
    • Search for a Range
    • Intersection of Two Arrays
    • Count of Smaller Numbers After Self
  • Binary Tree
    • 107. Binary Tree Level Order Traversal II
    • Lowest Common Ancestor of a Binary Tree
    • Lowest Common Ancestor of a Binary Tree II
    • Lowest Common Ancestor III
    • preorder Traversal
    • Inorder traversal
    • Binary Tree Path
    • post Order traversal
    • Level Traversal
    • Serialize and Deserialize Binary Tree 07/25
    • Find Leaves of Binary Tree 07/25
    • Sum of Left Leaves 07/25
    • Recover Binary Search Tree 07/26
    • Check Full Binary Tree 07/26
    • Binary Tree Longest Consecutive Sequence07/26
    • Equal Tree Partition 07/27
    • Same Tree 07/27
    • Sum Root to Leaf Numbers 07/26
    • Binary Search Tree Iterator 07/28
    • Preorder morris traversal 07/29
    • inorder traversal morris 07/29
  • BFS
    • Search Graph Nodes 07/30
    • Is Graph Bipartite? 07/30
    • Walls and Gates 07/30
    • Clone Graph 07/30
    • Word Ladder 07/30
    • Topological Sorting 08/01
    • Course Schedule 08/03
    • Course Schedule II 08/04
  • DFS
    • Target Sum 08/06
    • Minimum Subtree 08/07
    • Word Search 08/07
    • Pacific Atlantic Water Flow 08/08
    • Matrix Water Injection 08/10
    • Maximum Subtree 08/10
  • Dynamic Programming
    • 931. Minimum Falling Path Sum
    • Unique Binary Search Trees
  • Linked List
    • Reverse Linked List
    • Linked List Cycle
    • Swap Nodes in Pairs
    • Odd Even Linked List
    • Merge k Sorted Lists
    • Partition List
    • Palindrome Linked List
    • Reorder List
    • Linked List Cycle II
    • Delete Node in a Linked List
    • Reverse Nodes in k-Group
    • Rotate List
    • Reverse Linked List II
  • Arrays
    • 189. Rotate Array
    • 80. Remove Duplicates from Sorted Array II
    • 26. Remove Duplicates from Sorted Array
    • 628. Maximum Product of Three Numbers
    • 48. Rotate Image
    • 289. Game of Life
    • 334. Increasing Triplet Subsequence
    • 11. Container With Most Water
    • 122.Best Time to Buy and Sell Stock II
    • 274. H-Index
    • 134. Gas Station
    • 118. Pascal's Triangle
    • Sort Colors
    • Remove Element
    • Merge sorted array
    • First Missing Positive
  • Strings
    • 93. Restore IP Addresses
    • 71. Simplify Path
    • 43. Multiply Strings
    • 606. Construct String from Binary Tree
    • 917. Reverse Only Letters
    • 929.Unique Email Addresses
    • Valid Anagram
    • Compare Strings
    • Anagrams
    • Longest Common Prefix
    • Implement strStr()
    • String to Integer (atoi)
Powered by GitBook
On this page
  1. LinkedIn

Closest Binary Search Tree Value II

Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.

Note:

  • Given target value is a floating point.

  • You may assume k is always valid, that is: k ≤ total nodes.

  • You are guaranteed to have only one unique set of k values in the BST that are closest to the target.

Example:

Input: root = [4,2,5,1,3], target = 3.714286, and k = 2

    4
   / \
  2   5
 / \
1   3

Output: [4,3]

Follow up: Assume that the BST is balanced, could you solve it in less than O(n) runtime (where n = total nodes)?

O(N)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> closestKValues(TreeNode root, double target, int k) {
        List<Integer> res = new ArrayList<>();
        
        
        if(root == null)
            return res;
        
        Stack<TreeNode> stack = new Stack<>();
        
        TreeNode cur = root;
        
        while(!stack.isEmpty() || cur != null){
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            
            cur = stack.pop();
            if(res.size() < k){
                res.add(cur.val);
            }else{
                if(Math.abs(cur.val - target) < Math.abs(res.get(0) - target)){
                    res.add(cur.val);
                    res.remove(0);
                }
            }
            
            cur = cur.right;
        }
        
        return res;
        
    }
    
}

利用找到比target 小的,和比target大的数,时间复杂度o(klongN)

整体思路是把target cast到整形,找到第一个比target小的节点值,第一个比target大的节点值,根据绝对值来决定选择哪个,大的放到deque尾部,小的放到deque头部,把加入deque的那个节点 继续调用相应的方法 往两端扩散,比target大的节点就找一个再大一点的,比target小的节点 就找一个比他再小一点的节点

在找floor时,是找比target第一个小的数,当遇到节点比target小的时候,这个时候递归往右走一下,尝试寻找比当前节点大,比target小的数,如果存在,那么这个数与target的绝对值肯定比当前节点的数与target的绝对值小,如果返回空的话,说明当前节点是距离target最近的最小节点

同理,在找ceiling时,是找比target大的第一个数,当遇到节点小于target时,自然的往右走,不用特殊处理,当遇到节点大于target时,递归尝试往左走,试着找出一个比当前节点小,比target大的值,如果返回null,说明不存在,就返回当前节点。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> closestKValues(TreeNode root, double target, int k) {
       
        Deque<Integer> deque = new LinkedList<>();
        
        TreeNode floor = getFloor(root, (int)target);
        TreeNode ceiling = getCeiling(root,(int) target + 1);
        
        while(deque.size() < k && !(floor == null && ceiling == null)){
            if(floor == null || ceiling != null && Math.abs(floor.val - target) > Math.abs(ceiling.val - target)){
                deque.offerLast(ceiling.val);
                ceiling = getCeiling(root,ceiling.val+1);
            }else{
                deque.offerFirst(floor.val);
                floor = getFloor(root, floor.val-1);
            }
        }
        
        return new ArrayList<>(deque);
        
    }
    
    public TreeNode getFloor(TreeNode node, int target){
         if(node == null) {
            return null;
        }
        if(target == node.val) {
            return node;
        } else if(target < node.val) {
            return getFloor(node.left, target);
        } else {
            TreeNode n = getFloor(node.right, target);
            return n == null ? node : n;
        }
            
    }
    
     public TreeNode getCeiling(TreeNode node, int target){
         if(node == null) {
            return null;
        }
        if(target == node.val) {
            return node;
        } else if(target > node.val) {
            return getCeiling(node.right, target);
        } else {
            TreeNode n = getCeiling(node.left, target);
            return n == null ? node : n;
        }
            
    }
    
}

时间复杂度 o(k+logn)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> closestKValues(TreeNode root, double target, int k) {
        List<Integer> res = new ArrayList<>();
        
        if(root == null)
            return res;
        
        Stack<TreeNode> larger = new Stack<>();
        
        Stack<TreeNode> smaller = new Stack<>();
        
        TreeNode cur = root;
        
        while(cur != null){
            if(cur.val < target){
                smaller.push(cur);
                cur = cur.right;
            }else{
                larger.push(cur);
                cur = cur.left;
            }
        }
        
        while(res.size() < k ){
            double diffs = smaller.isEmpty()? Long.MAX_VALUE : Math.abs(smaller.peek().val - target);
            double diffl = larger.isEmpty() ? Long.MAX_VALUE : Math.abs(larger.peek().val-target);
            
            if(diffs < diffl){
                res.add(smaller.peek().val);
                getPrev(smaller);
            }else{
                res.add(larger.peek().val);
                getNext(larger);
            }
        }
        
        return res;
    }
    
    public void getPrev(Stack<TreeNode> stack){
        TreeNode node = stack.pop();
        
        TreeNode cur = node.left;
        
        while(cur != null){
            stack.push(cur);
            cur  = cur.right;
        }
    }
    
    public void getNext(Stack<TreeNode> stack){
        TreeNode node = stack.pop();
        
        TreeNode cur = node.right;
        
        while(cur  != null){
            stack.push(cur);
            cur = cur.left;
        }
    }
}

优化 时间复杂度 o(k+logn),空间复杂度 o(logN)

use two stack to save the treenode, one stack is for treenode val smaller than target, other one is for treenode val larger or equal with target, . then if the res.size < k, just get peek of the two stack, to compare the distance, after select one, update the stack. for example, I picked the stack from the smaller value stack, then I go to the stack.pop node's right, which is larger than the picked node. Then go to the node's left, this is trying to find the fist node larger than the peek(). same with larger stack.

为什么一开始push到stack以后 后面还要push?

因为一开始push的时候,只是围绕target值,对树进行二分查找,随着后面往res里放结果,需要已target的值为中心,往两边扩展塞进stack里备选,大的就找比他再大一点的,小的就找比他再小一点的,这些值在一开始都没有被压进stack

坑,当stack为空时,diff应该是Long.MAX_VALUE, 不然[0] Integer.MAX_VALUE, 1这个case会报异常

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> closestKValues(TreeNode root, double target, int k) {
        if(root == null )
            return null;
        
        Stack<TreeNode> prev = new Stack<>();
        Stack<TreeNode> next = new Stack<>();
        
        TreeNode cur = root;
        
        while(cur != null){
            if(cur.val < target){
                prev.push(cur);
                cur = cur.right;
            }else{
                next.push(cur);
                cur = cur.left;
            }
        }
        
        
        List<Integer> res = new ArrayList<>();
        
        while(res.size() < k){
            double diffP = prev.isEmpty() ? Long.MAX_VALUE : Math.abs(prev.peek().val - target);
            double diffN = next.isEmpty() ? Long.MAX_VALUE : Math.abs(next.peek().val-target);
            
            if(diffP < diffN){
                res.add(prev.peek().val);
                //找比现在这个小的
                getPrev(prev);
            }else{
                res.add(next.peek().val);
                //找比现在这个大的
                getNext(next);
            }
            
        }
        
        return res;
    }
    
    public void getPrev(Stack<TreeNode> prev){
        TreeNode node = prev.pop().left;
        
        while(node != null){
            prev.push(node);
            node = node.right;
        }
    }
    
    public void getNext(Stack<TreeNode> next){
        TreeNode node = next.pop().right;
        //node比现在的node要大,循环里找的是比node小的
        while(node != null){
            next.push(node);
            node =  node.left;
        }
    }
}

PreviousClosest Binary Search Tree ValueNextRotate String

Last updated 6 years ago