Skip to content

Commit 4c27c33

Browse files
committed
Problem 176: Closest value in a BST from a target
1 parent 760bec0 commit 4c27c33

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 175 |
9+
| Total Problems | 176 |
1010

1111
</center>
1212

@@ -131,6 +131,7 @@ Include contains single header implementation of data structures and some algori
131131
| Find kth smallest element in a binary search tree | [kth_smallest.cpp](tree_problems/kth_smallest.cpp)|
132132
| Validate if a given binary tree is a binary search tree. | [validate_bst.cpp](tree_problems/validate_bst.cpp) |
133133
| Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.| [find_target_k.cpp](tree_problems/find_target_k.cpp) |
134+
| Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Also, to note that the target value is a floating point. There will be only one unique value which is closest to the target. |[closest_bst_value.cpp](tree_problems/closest_bst_value.cpp) |
134135

135136
### String Problems
136137
| Problem | Solution |

tree_problems/closest_bst_value.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Given a non-empty binary search tree and a target value,
3+
* find the value in the BST that is closest to the target.
4+
* Also, to note that the target value is a floating point.
5+
* There will be only one unique value which is closest to the target.
6+
*/
7+
8+
#include <iostream>
9+
#include <limits>
10+
#include <cmath>
11+
12+
struct TreeNode {
13+
int data;
14+
TreeNode* left;
15+
TreeNode* right;
16+
TreeNode(int d):
17+
data{d}, left{nullptr}, right{nullptr}{}
18+
};
19+
20+
21+
22+
int closest_bst_value(TreeNode* root, double target)
23+
{
24+
// Tree is non-empty, so root is always valid.
25+
int curr_value = root->data;
26+
TreeNode* child = (target < curr_value) ? (root->left) : (root->right);
27+
if (!child) {
28+
return curr_value;
29+
}
30+
int child_value = closest_bst_value(child, target);
31+
return std::abs(curr_value - target) <
32+
std::abs(child_value- target) ? curr_value : child_value;
33+
}
34+
35+
int closest_bst_value_iterative(TreeNode* root, double target)
36+
{
37+
int closest = root->data;
38+
while (root != nullptr)
39+
{
40+
if (std::abs(target - closest) >= std::abs(target - root->data)) {
41+
closest = root->data;
42+
}
43+
root = target < root->data ? root->left : root->right;
44+
}
45+
return closest;
46+
}
47+
48+
void print_inorder(TreeNode* root)
49+
{
50+
if (root != nullptr) {
51+
print_inorder(root->left);
52+
std::cout << root->data << " ";
53+
print_inorder(root->right);
54+
}
55+
}
56+
57+
int main()
58+
{
59+
TreeNode* root = new TreeNode(10);
60+
root->left = new TreeNode(5);
61+
root->right = new TreeNode(15);
62+
root->left->left = new TreeNode(2);
63+
root->left->right = new TreeNode(7);
64+
root->right->left = new TreeNode(12);
65+
root->right->right = new TreeNode(16);
66+
67+
std::cout << "Inorder traversal of tree: ";
68+
print_inorder(root);
69+
std::cout << std::endl;
70+
std::cout << "Closest value from 8.6778 is :" << closest_bst_value(root, 3.6778)
71+
<< std::endl;
72+
73+
std::cout << "(Iterative) Closest value from 8.6778 is :"
74+
<< closest_bst_value_iterative(root, 3.6778) << std::endl;
75+
return 0;
76+
}

0 commit comments

Comments
 (0)