Bird
Raised Fist0
Interview Prepbinary-search-treesmediumAmazonFacebookGoogleMicrosoft

Validate Binary Search Tree

Choose your preparation mode3 modes available
📋
Problem

Imagine you are building a database index that relies on a binary search tree structure. You need to verify if the tree is correctly structured to ensure fast lookups.

Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: - The left subtree of a node contains only nodes with keys less than the node's key. - The right subtree of a node contains only nodes with keys greater than the node's key. - Both the left and right subtrees must also be binary search trees. Input: root node of a binary tree. Output: true if the tree is a valid BST, false otherwise.

The number of nodes in the tree is in the range [1, 10^5].Node values are integers and can be negative or positive.You must solve the problem with O(n) time complexity.
Edge cases: Single node tree → trueTree with duplicate values → false (BST requires strictly less/greater)Left skewed tree (all nodes have only left child) → true if values strictly decreasing
</>
IDE
def is_valid_bst(root: Optional[TreeNode]) -> bool:public boolean isValidBST(TreeNode root)bool isValidBST(TreeNode* root)function isValidBST(root)
def is_valid_bst(root):
    # Write your solution here
    pass
class Solution {
    public boolean isValidBST(TreeNode root) {
        // Write your solution here
        return false;
    }
}
#include <vector>
using namespace std;

bool isValidBST(TreeNode* root) {
    // Write your solution here
    return false;
}
function isValidBST(root) {
    // Write your solution here
}
0/9
Common Bugs to Avoid
Wrong: trueCode only compares immediate children to parent, missing violations deeper in subtrees.Use recursive min/max bounds to validate all descendants, not just direct children.
Wrong: trueCode allows duplicate values by using <= or >= instead of strict < and > comparisons.Change comparisons to strict inequalities: left < node < right.
Wrong: falseCode fails on single node trees due to missing base case or null pointer errors.Return true immediately for null or leaf nodes.
Wrong: TLECode uses O(n^2) brute force approach checking subtree min/max repeatedly.Implement O(n) solution using min/max range validation or inorder traversal.
Test Cases
t1_01basic
Input{"root":[2,1,3]}
Expectedtrue

The tree: 2 / \ 1 3 All left nodes are less than 2 and all right nodes are greater than 2.

t1_02basic
Input{"root":[5,1,7,null,null,6,8]}
Expectedtrue

The tree: 5 / \ 1 7 / \ 6 8 All left nodes less than 5, right nodes greater than 5, and subtrees valid BSTs.

t2_01edge
Input{"root":[1]}
Expectedtrue

Single node tree is always a valid BST.

t2_02edge
Input{"root":[2,2,2]}
Expectedfalse

Tree with duplicate values violates strict BST property (left < node < right).

t2_03edge
Input{"root":[3,2,null,1]}
Expectedtrue

Left skewed tree with strictly decreasing values: 3 -> 2 -> 1 is a valid BST.

t3_01corner
Input{"root":[10,5,15,null,null,6,20]}
Expectedfalse

Right subtree has a node (6) less than root (10), violating BST property.

t3_02corner
Input{"root":[1,null,1]}
Expectedfalse

Right child equals root value, violating strict greater-than rule.

t3_03corner
Input{"root":[5,4,6,null,null,3,7]}
Expectedfalse

Node with value 3 in right subtree of 5 is less than 5, violating BST property.

t4_01performance
Input{"root":[1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9,null,10,null,11,null,12,null,13,null,14,null,15,null,16,null,17,null,18,null,19,null,20,null,21,null,22,null,23,null,24,null,25,null,26,null,27,null,28,null,29,null,30,null,31,null,32,null,33,null,34,null,35,null,36,null,37,null,38,null,39,null,40,null,41,null,42,null,43,null,44,null,45,null,46,null,47,null,48,null,49,null,50,null,51,null,52,null,53,null,54,null,55,null,56,null,57,null,58,null,59,null,60,null,61,null,62,null,63,null,64,null,65,null,66,null,67,null,68,null,69,null,70,null,71,null,72,null,73,null,74,null,75,null,76,null,77,null,78,null,79,null,80,null,81,null,82,null,83,null,84,null,85,null,86,null,87,null,88,null,89,null,90,null,91,null,92,null,93,null,94,null,95,null,96,null,97,null,98,null,99,null,100]}
Expectedtrue

Right skewed tree with n=100 nodes, strictly increasing values. O(n) time complexity required to pass within 2 seconds.