KatInterview

783. Search in a Binary Search Tree

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to find a node in a Binary Search Tree (BST) that has a specific value, val. If such a node exists, we must return the entire subtree rooted at that node. If it doesn't exist, we should return null. The core of this problem is to efficiently utilize the properties of a BST.

Solution

Search in a Binary Search Tree in Python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
        # Iterate while the root is not None and we haven't found the value.
        while root and root.val != val:
            # If the value is smaller than the current node's value, search in the left subtree.
            if val < root.val:
                root = root.left
            # Otherwise, search in the right subtree.
            else:
                root = root.right
        # Return the found node or None if the value is not in the tree.
        return root

Search in a Binary Search Tree in Java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        // Iterate as long as the current node is not null and its value is not the target value.
        while (root != null && root.val != val) {
            // If val is smaller than the current node's value, go to the left child.
            // Otherwise, go to the right child.
            root = (val < root.val) ? root.left : root.right;
        }
        // Return the found node, or null if the value is not in the tree.
        return root;
    }
}

Search in a Binary Search Tree in C++

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* searchBST(TreeNode* root, int val) {
        // Iterate as long as the current node is not null and its value is not the target value.
        while (root != nullptr && root->val != val) {
            // If the target value is less than the current node's value,
            // move to the left child. Otherwise, move to the right child.
            root = (val < root->val) ? root->left : root->right;
        }
        // At the end of the loop, root is either the node we're looking for,
        // or null if the value is not in the tree.
        return root;
    }
};

Search in a Binary Search Tree in TypeScript

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function searchBST(root: TreeNode | null, val: number): TreeNode | null {
    // Iterate as long as the current node is not null and its value is not the target value.
    while (root !== null && root.val !== val) {
        // If val is smaller than the current node's value, go to the left child.
        // Otherwise, go to the right child.
        root = (val < root.val) ? root.left : root.right;
    }
    // Return the found node, or null if the value is not in the tree.
    return root;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.