Search in a Binary Search Tree
Start at the root. If val is smaller than the current value, the answer can only be on the left, so the right side is thrown away. If it is bigger, go right. Stop when the value matches or the walk runs off the tree.
The answer, if it exists, is always inside the subtree rooted at cur.
Problem
You are given the root of a binary search tree and a number val.
Find the node whose value is val, and return that node. The node keeps its children, so its whole subtree comes back with it. If no node holds val, return null.
The root of a binary search tree (between 1 and 5000 nodes) and an integer val.
The node whose value is val (the subtree rooted at it), or null if no node has that value.
Node 2 holds the value we want, so we return it. Its subtree is 2 with children 1 and 3.
No node holds 5. The walk goes 4 β 7, then 7 has no left child, so it runs off the tree.
- β’ The number of nodes is between 1 and 5000
- β’ 1 β€ Node.val β€ 10^7
- β’ root is a binary search tree
- β’ 1 β€ val β€ 10^7
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck β each hint is counted.
- 1.
The tree is a binary search tree. If I stand at one node and compare
valwith its value, what does that tell me about the two sides? - 2.
Could
valsit in the right subtree of a node whose value is bigger thanval? Why or why not? - 3.
Which two things can end my search, and what does each one mean for the answer?
- 4.
Do I need recursion or a stack here, or is a single pointer enough?
- 5.
On a chain of 5000 nodes that leans to the right, how many nodes can the search visit? What changes for a balanced tree?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Ignore the BST order: check a node, then search its left and right subtrees | O(N) | O(H) |
| Optimal | Compare `val` with the current node, then step to the one child that can hold it | O(H) | O(1) |
Why does this belong to this pattern?
- βbinary search treeβLeft < node < right. One comparison tells you which side can hold the target.
- βreturn the node with value valβReturn the node itself, not a copy. Its subtree comes with it.
- βnot found β nullβThe walk must stop cleanly when it falls off the tree.
This is binary search on a tree. Each comparison throws away one whole side, the same way binary search throws away half of a sorted array. The tree version needs no stack or recursion, because each step keeps only one candidate: the current node. The search does not need the other side because the BST order already rules it out.
Don't make this mistake
The two-sided search still returns the right node, so a small test passes. But it throws away the BST order. On [5,3,8,2,4,7,9,1] with val = 6, the two-sided search visits all 8 nodes. The BST walk goes 5 β 8 β 7 and stops after 3.
The two sides are swapped, so the walk goes the wrong way. On [5,3,8,2,4,7,9,1] with val = 4: 4 < 5, but the code goes right to 8, then left to 7, then null. It returns null even though 4 is in the tree.
The value is right, but a new node has no children, so the subtree is lost. The answer is the existing node. On [4,2,7,1,3] with val = 2, the copy would lose the children 1 and 3.
When val is not in the tree, cur becomes null and cur->val crashes. On [4,2,7,1,3] with val = 5, the walk goes 4 β 7 β null, and the check on null is the crash.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.