Command Palette Search for a command to run...
SD PATH DKR LNX NET GIT K8S CI AWS TF OPS KFK DB RDS BF DBD IDP OBS SEC SRE DSA AI Problem 17.1 · Binary Search Trees Easy
What it teaches: Following one path down the tree using the ordering.
Practise it on judges as “Search in a Binary Search Tree”.
The problem Return the subtree rooted at the node whose value equals val, or null if there is no such node.
Example 1
Input: root = [4, 2, 7, 1, 3], val = 2
Output: [2, 1, 3]Example 2
Input: root = [4, 2, 7, 1, 3], val = 5
Output: []Constraints
1 ≤ nodes ≤ 5000 Values are distinct Pattern clues in the wording
→ BST + look up one value These clues point to BST Ordering : Use left < node < right to skip half the tree, and in-order traversal to visit values in sorted order.
Stuck? Take one hint at a time
Solution.java · starter Java Python C++ JavaScript Go
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
return null;
}
}Write your solution locally or in your editor for now. The in-browser runner (Java first, then Python, C++ and more) will run these tests right here.
Test cases
# Input Expected 1 root = [4,2,7,1,3]
val = 2
[2,1,3] 2 root = [4,2,7,1,3]
val = 5
[]
+ 1 hidden test the code runner will check
Mark complete
Previous lessonIn-Order Tricks and Deleting Next problem Insert into a Binary Search Tree