# 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 = 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.
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);
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:
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:
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
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
// Define BST classclass 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; }}
// BSTconst 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