Search in a Binary Search Tree - Problem
Search in a Binary Search Tree is a fundamental tree traversal problem that leverages the powerful properties of Binary Search Trees (BSTs).

You are given the root of a binary search tree and an integer val. Your task is to find the node in the BST where the node's value equals val and return the entire subtree rooted at that node. If no such node exists, return null.

Remember: In a BST, for every node, all values in the left subtree are smaller than the node's value, and all values in the right subtree are larger than the node's value. This property allows us to efficiently navigate the tree without checking every node!

Goal: Find and return the subtree starting from the target node
Input: Root of BST + target value
Output: Subtree rooted at target node, or null if not found

Input & Output

example_1.py โ€” Basic Search
$ Input: root = [4,2,7,1,3], val = 2
โ€บ Output: [2,1,3]
๐Ÿ’ก Note: The node with value 2 exists in the BST. We return the subtree rooted at this node, which includes nodes 2, 1, and 3.
example_2.py โ€” Value Not Found
$ Input: root = [4,2,7,1,3], val = 5
โ€บ Output: null
๐Ÿ’ก Note: The value 5 does not exist in the BST, so we return null. The search would go: 5 > 4 (go right to 7), 5 < 7 (go left to null).
example_3.py โ€” Single Node Tree
$ Input: root = [1], val = 1
โ€บ Output: [1]
๐Ÿ’ก Note: The tree has only one node with value 1, which matches our target. We return the single-node subtree.

Visualization

Tap to expand
LIBRARYSection 4Section 2Books 1-3Section 7Books 5-9Book 3FOUND!Searching for Book #3:1. At Section 4: 3 < 4, go LEFT to Section 22. At Section 2: 3 > 2, go RIGHT to find Book 33. Found Book 3! Return this section.Total steps: 2 (instead of checking all 5 books)
Understanding the Visualization
1
Enter Library at Main Desk
Start at the root - this is like the main information desk that directs you to different sections
2
Check Section Sign
Compare your book number with the current section. The sign tells you the range of books here
3
Choose Direction
Go left for smaller numbers, right for larger numbers, or stay if you found your section
4
Repeat Until Found
Keep following the signs until you reach your target book or a dead end (book doesn't exist)
Key Takeaway
๐ŸŽฏ Key Insight: BST property allows us to eliminate half the search space at each step, making search as efficient as a well-organized library system!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(h)

Height of the tree h. For balanced BST h = O(log n), for skewed BST h = O(n)

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Iterative version uses constant extra space, recursive version uses O(h) for call stack

n
2n
โœ“ Linear Space

Constraints

  • The number of nodes in the tree is in the range [0, 5000]
  • 1 โ‰ค Node.val โ‰ค 107
  • root is a binary search tree
  • 1 โ‰ค val โ‰ค 107
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28 Apple 25
82.1K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen