Search in a Binary Search Tree
Locate a target value in a Binary Search Tree recursively by steering left or right based on the BST ordering property.
01The problem
You are given the root of a binary search tree (BST) and an integer val.
Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return null.
| Input | Output | Why |
|---|---|---|
root = [4, 2, 7, 1, 3], val = 2 | [2, 1, 3] | Node with value 2 is found; returns its full subtree |
root = [4, 2, 7, 1, 3], val = 5 | [] (null) | Value 5 does not exist in the tree |
Constraints: Number of nodes in range [1, 5000], 1 <= Node.val <= 10^7, root is a valid BST.
Loading the rest of the lesson…