Search in a Binary Search Tree (LeetCode 700): Solution and Explanation
Given the root of a binary search tree and a value, return the subtree rooted at the node holding that value, or null if no node has it.
Intuition
A BST keeps every value smaller than a node in its left subtree and every larger value in its right subtree. Read left to right, the nodes are in sorted order.
So a single comparison tells us which side val must be on. If val is smaller than the current node, nothing on the right can match, and we never look there. This is binary search, with the tree's shape deciding where each split falls.
We keep moving down one child at a time. Either we land on val, or the child we need is null, which means the spot where val would have to be is empty and it is not in the tree.
Approach: Iterative descent
- Start with
rootat the tree's root. - While
rootis not null androot.val != val: ifval < root.valsetroot = root.left, otherwise setroot = root.right. - Return
root. It is the matching node, whose subtree is the answer, or null.
Step-by-step example
root = [4,2,7,1,3], val = 2
- At 4: 2 < 4, so the answer can only be in the left subtree. 7 is ruled out.
- Move to 2: it equals val, so the loop stops.
- Return node 2. Serialized, its subtree is [2,1,3].
Code: Python, Java, C++
1class Solution:
2 def searchBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
3 while root and root.val != val:
4 if val < root.val:
5 root = root.left
6 else:
7 root = root.right
8 return root1class Solution {
2 public TreeNode searchBST(TreeNode root, int val) {
3 while (root != null && root.val != val) {
4 root = val < root.val ? root.left : root.right;
5 }
6 return root;
7 }
8}1class Solution {
2public:
3 TreeNode* searchBST(TreeNode* root, int val) {
4 while (root && root->val != val) {
5 root = val < root->val ? root->left : root->right;
6 }
7 return root;
8 }
9};Time and space complexity
O(h)
Each step goes one level down, so at most h + 1 nodes are visited, where h is the height. That is O(log n) for a balanced tree and O(n) for a chain.
O(1)
The loop only keeps a single pointer. A recursive version would use O(h) stack space instead.
Other approaches
Recursive search (time O(h), space O(h))
Return root if it is null or matches, otherwise recurse into the one child that can contain val. Same idea, but it uses the call stack.
Full traversal (time O(n), space O(h))
Visit every node with DFS or BFS until val is found. Correct for any binary tree, but it ignores the BST ordering.
Common mistakes
- Searching both subtrees. It still works, but it turns an O(h) search into O(n).
- Reading
root.valbefore checking thatrootis not null. The null check must come first in the loop condition. - Returning
[]or a new node instead of the existing node. The answer is the original subtree.
Frequently asked questions
Why return the whole subtree instead of just the node's value?
The function returns a TreeNode, and a node carries its children with it. The judge serializes everything reachable from it, so the output shows the subtree.
What is the worst case?
A tree built from sorted input becomes a chain of height n, so the search can visit every node. Balanced trees keep the height near log n.
Is recursion or iteration better here?
Both run in O(h) time. Iteration needs only O(1) extra space, which matters for a deep, chain-like tree.