https://github.com/emahtab/closest-binary-search-tree-value
Closest Binary Search Tree Value
https://github.com/emahtab/closest-binary-search-tree-value
binary-search-tree leetcode problem-solving
Last synced: 2 months ago
JSON representation
Closest Binary Search Tree Value
- Host: GitHub
- URL: https://github.com/emahtab/closest-binary-search-tree-value
- Owner: eMahtab
- Created: 2020-02-09T12:51:13.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-10-18T13:49:32.000Z (about 1 year ago)
- Last Synced: 2025-02-02T03:26:15.856Z (9 months ago)
- Topics: binary-search-tree, leetcode, problem-solving
- Size: 10.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Closest Binary Search Tree Value
## https://leetcode.com/problems/closest-binary-search-tree-valueGiven a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.
**Note:**
1. Given target value is a floating point.
2. You are guaranteed to have only one unique value in the BST that is closest to the target.
```
Example:Input: root = [4,2,5,1,3], target = 3.714286
4
/ \
2 5
/ \
1 3Output: 4
But if target is 3.5 and we are asked to return the smallest number which is closest to target,
then answer will be 3 and not 4 (both 3 and 4 have the same difference from target but 3 is smaller than 4) .
```
# Implementation 1 : O(nlogn), inorder traversal, sort
```java
class Solution {
public int closestValue(TreeNode root, double target) {
List nums = new ArrayList();
inorder(root, nums);
Collections.sort(nums, (n1,n2) ->
Double.compare(Math.abs(n1 - target), Math.abs(n2 - target)));
return nums.get(0);
}public void inorder(TreeNode node, List nums) {
if (node == null) return;
inorder(node.left, nums);
nums.add(node.val);
inorder(node.right, nums);
}
}
```# Implementation 2 : O(h)
```java
class Solution {
public int closestValue(TreeNode root, double target) {
int closestValue = root.val;
TreeNode node = root;
while(node != null){
if(Math.abs(node.val - target) < Math.abs(closestValue - target))
closestValue = node.val;
node = target < node.val ? node.left : node.right;
}
return closestValue;
}
}
```
# Implementation 3 : O(h), If there are multiple answers, return the smallest number
```java
class Solution {
public int closestValue(TreeNode root, double target) {
int closestValue = root.val;
TreeNode node = root;
while(node != null){
if(Math.abs(node.val - target) < Math.abs(closestValue - target) ||
(Math.abs(node.val - target) == Math.abs(closestValue - target) && node.val < closestValue))
closestValue = node.val;
node = target < node.val ? node.left : node.right;
}
return closestValue;
}
}
```# References :
https://www.youtube.com/watch?v=_sz0Y4g1Goshttps://leetcode.com/problems/closest-binary-search-tree-value/editorial