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
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
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
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)
โ Linear Growth
Space Complexity
O(1)
Iterative version uses constant extra space, recursive version uses O(h) for call stack
โ Linear Space
Constraints
-
The number of nodes in the tree is in the range
[0, 5000] -
1 โค Node.val โค 107 -
rootis a binary search tree -
1 โค val โค 107
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code