Nearest/Closest value in BST
Purpose is to find the nearest value to the target node in the binary search tree.
Approach
We will define a INT_MAX variable which will store the difference between target and current node value.
We will try to minimize the difference, and the node with which we will get the minimum difference will be the answer.
We will store minimum value(which is currently INT_MAX) and answer node in array named curr.
i.e. curr[0] contains minimum value
curr[1] contains answer(nearest node)
code link - closest value in bst
Please leave comment for any suggestions or topic you would like to read.
Comments
Post a Comment
If you have any doubt, Please let me know