Close
Topic list
Closest Binary Search Tree Value
EASY
15 mins
Binary Search Trees
Topics (Covered in this problem)
Problem solved
Skill meter
Binary Search Trees
-
-
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Closest Binary Search Tree Value

Contributed by
Prashansa
Easy
0/40
Avg time to solve 15 mins
Success Rate 85 %
Share

## Problem Statement

#### A binary search tree (BST) is a binary tree data structure with the following properties.

``````• The left subtree of a node contains only nodes with data less than the node’s data.

• The right subtree of a node contains only nodes with data greater than the node’s data.

• Both the left and right subtrees must also be binary search trees.
``````
##### For Example:

``````For the given BST and target value ‘K’ =  32, the closest element is 30 as the absolute difference between 30 and 32 (|32 - 30|) is the minimum among all other possible node-target pairs.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1:
``````1
6 3 9 2 5 -1 10 -1 -1 -1 -1 -1 -1
4
``````
##### Sample Output 1:
``````3
``````
##### Explanation For The Sample Output 1:

``````The minimum possible absolute difference between the given target and node is 1 for nodes 3 and 5. Since 3 is smaller than 5, the output is 3.
``````
##### Sample Input 2:
``````2
3 1 5 -1 2 -1 -1 -1 -1
4
9 6 10 4 7 -1 11 -1 -1 -1 -1 -1 -1
100
``````
##### Sample Output 2:
``````3
11
``````
Auto
Console