# LeetCode: Find Closest Value In BST

Table of Contents

Statement

Part 1

Letโ€™s think about this problem. The first step is to understand what a BST (Binary Search Tree) is. In simple words, itโ€™s a data structure designed to make searching efficient. For example, in a tree with values 10, 5, and 15, each value is a node. Each node can have up to two children: one on the left and one on the right. Because of this ordering and the BST rule, you can ignore large parts of the tree while searching. For example, if youโ€™re looking for 15, you compare it with 10. Since 15 > 10, you skip the left subtree and go right. That ordering means you donโ€™t have to check every nodeโ€”you can discard half the tree at each step. Thatโ€™s the power of a BST.

This is an example how it looks a BST tree:

tree
tree = 10
/ \
5 15
/ \ / \
2 5 13 22
/ \
1 14

Part 2

Now that you know what a BST is, we can look at how to represent it in JavaScript. There are different ways to do this. In this case, Iโ€™ll show you two methods, but if youโ€™re curious, you can explore other approaches on the internet.

BST method 1

In this method, we create a class called BST. A class works as a reusable blueprint for making nodes consistently and initializing their values with the constructor. When you create an instance of the class using new, the constructor is automatically called and an object is created with the given information.

You can think of a class like a template or schema: you provide the values, and it returns an object filled with those values.

tree
class BST {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
const root = new BST(10);
root.left = new BST(5);
root.left.left = new BST(2);
root.left.left.left = new BST(1);
root.left.right = new BST(5);
root.right = new BST(15);
root.right.left = new BST(13);
root.right.left.right = new BST(14);
root.right.right = new BST(22);
tree
const root = new BST(10);
root.left.left.left = new BST(1);
TypeError: Cannot set property 'left' of null

If you run code like this, it wonโ€™t work and youโ€™ll get an error such as:

tree
TypeError: Cannot set property 'left' of null

This happens because you always have to define the parent nodes before the child nodes.

For example, at first you might have:

tree
const BST = { value: 12, left: null, right: null }

BST method 2

In this method, we donโ€™t use a class. Instead, we use a JSON object in its basic form. In this case, we canโ€™t create an instance of a class using new and a constructor to initialize values. We always have to manually set the values ourselves

tree
let JsonBST = { value: 12, left: null, right: null }
JsonBST.left = { value: 15, left: null, right: null }
JsonBST.left.left = { value: 20, left: null, right: null }

Soluction

hello.py
// Define BST class
class BST {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
function findClosestValueInBst(tree, target) {
return findClosestValueInBstHelper(tree, target, tree.value);
}
function findClosestValueInBstHelper(tree, target, closestValue) {
if (tree === null) return closestValue;
if (Math.abs(tree.value - target) < Math.abs(closestValue - target)) {
closestValue = tree.value;
}
if (target < tree.value && tree.left !== null) {
return findClosestValueInBstHelper(tree.left, target, closestValue);
} else if (target > tree.value && tree.right !== null) {
return findClosestValueInBstHelper(tree.right, target, closestValue);
} else {
return closestValue;
}
}
// BST
const root = new BST(10);
root.left = new BST(5);
root.left.left = new BST(2);
root.left.left.left = new BST(1);
root.left.right = new BST(5);
root.right = new BST(15);
root.right.left = new BST(13);
root.right.left.right = new BST(14);
root.right.right = new BST(22);
console.log(findClosestValueInBst(root, 12)); // โœ… Expected: 13
My avatar

Appreciate you reading. If you want more hacking write-ups, network labs, and code deep-dives, check out my other posts or connect via the social links in the footer.


More Posts