easyBinary Search TreeTrees 0 views
Search in a Binary Search Tree
Find the node with a given value in a binary search tree and return the subtree rooted there.
Given the root of a binary search tree (BST) and an integer target, find the node in the BST whose value equals target.
Return the subtree rooted at that node. If no node has that value, return an empty tree.
Example 1
Input: root = [5,3,8,2,4,7,9], target = 3
Output: [3,2,4]
Example 2
Input: root = [5,3,8,2,4,7,9], target = 10
Output: []
Explanation: No node has value 10, so the result is an empty tree.
Example 3
Input: root = [], target = 5
Output: []
Explanation: Searching an empty tree always fails.
Constraints
- The number of nodes in the tree is in the range [0, 5000].
- 1 <= Node.val <= 100000
- root is guaranteed to be a valid binary search tree.
- All node values are unique.
Hints
Companies
No companies reported yet.
Discussion
Sign in to join the discussion.
Loading discussion...
Test results
No test cases yet.