Algo-Sim › Binary Trees & BST
Validate Binary Search Tree
Medium · Binary Trees & BST · step through it in Java, Python and C++ on your own input
Pass each node the open range (low, high) its ancestors allow. The left child tightens high, the right child tightens low.
Problem
Decide whether a binary tree is a valid binary search tree: for every node, all values in its left subtree are strictly smaller than it and all values in its right subtree are strictly larger, and both subtrees are themselves valid.
Constraints
- 1 ≤ number of nodes ≤ 10⁴
- -2³¹ ≤ Node.val ≤ 2³¹ − 1
- Checking only each node against its children is not enough
Edge cases to test
- Duplicates: invalid (strictly increasing)
- A deep node that breaks an ancestor’s bound
Integer.MIN_VALUEandMAX_VALUEas node values
Follow-up questions
- Why not just compare each node with its children?
- A grandchild can break an ancestor’s bound. Passing (low, high) down, or checking inorder is increasing, catches it.
- How do you handle the int limits?
- Use null (or long) bounds instead of
MIN_VALUE/MAX_VALUEsentinels.
Solution
Java
public boolean isValidBST(TreeNode root) {
return valid(root, null, null);
}
private boolean valid(TreeNode node, Integer low, Integer high) {
if (node == null) return true;
if (low != null && node.val <= low) return false;
if (high != null && node.val >= high) return false;
if (!valid(node.left, low, node.val)) return false;
return valid(node.right, node.val, high);
}Python
def is_valid_bst(root):
def valid(node, low, high):
if not node:
return True
if low is not None and node.val <= low:
return False
if high is not None and node.val >= high:
return False
if not valid(node.left, low, node.val):
return False
return valid(node.right, node.val, high)
return valid(root, None, None)C++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool isValidBST(TreeNode *root) {
return valid(root, LLONG_MIN, LLONG_MAX);
}
bool valid(TreeNode *node, long long low, long long high) {
if (node == nullptr)
return true;
if (node->val <= low)
return false;
if (node->val >= high)
return false;
if (!valid(node->left, low, node->val))
return false;
return valid(node->right, node->val, high);
}
};
More Binary Trees & BST problems
- Preorder Traversal (Recursive) (Easy)
- Inorder Traversal (Recursive) (Easy)
- Postorder Traversal (Recursive) (Easy)
- Preorder Traversal (Iterative) (Easy)
- Inorder Traversal (Iterative) (Easy)
- Postorder Traversal (Iterative) (Easy)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Binary Tree Right Side View (Medium)
- Vertical Order Traversal of a Binary Tree (Hard)
- Maximum Depth of Binary Tree (Easy)
- Diameter of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Invert Binary Tree (Easy)
- Flatten Binary Tree to Linked List (Medium)
- Same Tree (Easy)
- Symmetric Tree (Easy)
- Subtree of Another Tree (Easy)
- Path Sum (Easy)
- Path Sum II (Medium)
- Path Sum III (Medium)
- Count Good Nodes in Binary Tree (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Kth Ancestor of a Tree Node (Hard)
- All Nodes Distance K in Binary Tree (Medium)
- Search in a Binary Search Tree (Easy)
- Insert into a Binary Search Tree (Medium)
- Delete Node in a BST (Medium)
- Kth Smallest Element in a BST (Medium)
- Binary Search Tree Iterator (Medium)
- Convert Sorted Array to Binary Search Tree (Easy)
- AVL Tree Insert (Hard)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Binary Tree Maximum Path Sum (Hard)
- Serialize and Deserialize Binary Tree (Hard)