Loading…
Loading…
Given a binary tree, print true if it's a valid BST (every node's value is strictly greater than every value in its left subtree and strictly less than every value in its right subtree), false otherwise.
Input: 5 1 4 null null 3 6
Output: false (5's right subtree contains 3, which is smaller than 5)
The classic trap: checking only "left child < node < right child" locally is not sufficient — a node deep in the left subtree could still violate the BST property relative to an ancestor several levels up. Instead, carry a valid (low, high) range down through the recursion, tightening it at every step.
Line 1: the tree in level-order, space-separated, null for missing children
Print true or false.
Input (stdin)
Output
Input (stdin)
Output
Sign in to track solved problems and earn XP.