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.3 · Binary Search Trees Medium
What it teaches: Passing an allowed range down the tree.
Practise it on judges as “Validate Binary Search Tree”.
The problem Return true if the tree is a valid BST: every left subtree holds only smaller values, every right subtree only larger values (strictly), at every node.
Example 1
Input: root = [2, 1, 3]
Output: trueExample 2
Input: root = [5, 1, 4, null, null, 3, 6]
Output: falseConstraints
1 ≤ nodes ≤ 10⁴ −2³¹ ≤ value ≤ 2³¹ − 1 Pattern clues in the wording
→ Check an ordering property across the whole tree 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 boolean isValidBST(TreeNode root) {
return false;
}
}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 = [2,1,3]
true 2 root = [5,1,4,null,null,3,6]
false 3 root = [5,4,6,null,null,3,7]
3 is in 5's right subtree
false
+ 2 hidden tests the code runner will check
Mark complete
Previous problemInsert into a Binary Search Tree Next problem Kth Smallest Element in a BST