What if you could instantly know if your tree is perfectly organized without checking every single node by hand?
Why Validate if Tree is BST in DSA Typescript?
Imagine you have a family tree drawn on paper, and you want to check if it follows a special rule: every child on the left is smaller than the parent, and every child on the right is bigger. Doing this by looking at each person one by one is tiring and confusing.
Checking each node manually means you might miss some mistakes or spend hours comparing every child with its parent and other relatives. It's easy to get lost and make errors, especially if the tree is big.
Using a simple step-by-step check called "Validate if Tree is BST" helps you quickly and correctly confirm if the tree follows the rules. It looks at each node and makes sure all left children are smaller and all right children are bigger, without missing anything.
function isBST(node) {
if (!node) return true;
if (node.left && node.left.value >= node.value) return false;
if (node.right && node.right.value <= node.value) return false;
return isBST(node.left) && isBST(node.right);
}function isBST(node, min = -Infinity, max = Infinity) {
if (!node) return true;
if (node.value <= min || node.value >= max) return false;
return isBST(node.left, min, node.value) && isBST(node.right, node.value, max);
}This method lets you confidently check large trees for correctness quickly and without mistakes.
When building a search system, you want to make sure your data is organized correctly so you can find things fast. Validating a tree as a BST ensures your search will work perfectly.
Manual checks are slow and error-prone for big trees.
Validating BST uses simple rules to check all nodes efficiently.
This helps keep data organized and easy to search.
Practice
Solution
Step 1: Understand BST property
A BST requires that for each node, all values in the left subtree are smaller and all values in the right subtree are larger.Step 2: Compare options with definition
Every node's left subtree has smaller values and right subtree has larger values matches the BST property exactly, while others describe different tree properties.Final Answer:
Every node's left subtree has smaller values and right subtree has larger values -> Option DQuick Check:
BST property = Every node's left subtree has smaller values and right subtree has larger values [OK]
- Confusing BST with balanced tree
- Thinking all nodes must have two children
- Assuming all values are equal
Solution
Step 1: Identify correct parameter and return types
The function should accept a tree node or null and return a boolean indicating if it is a BST.Step 2: Match options with correct signature
function isBST(root: TreeNode | null): boolean matches this: input is TreeNode or null, output is boolean. Others have wrong types or return types.Final Answer:
function isBST(root: TreeNode | null): boolean -> Option BQuick Check:
Correct signature = function isBST(root: TreeNode | null): boolean [OK]
- Using wrong parameter type like number
- Returning number instead of boolean
- Using void return type
class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function isBST(root: TreeNode | null, min: number = -Infinity, max: number = Infinity): boolean {
if (!root) return true;
if (root.val <= min || root.val >= max) return false;
return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}
const tree = new TreeNode(10, new TreeNode(5), new TreeNode(15, new TreeNode(6), new TreeNode(20)));
console.log(isBST(tree));Solution
Step 1: Analyze the tree structure
The root is 10, left child 5 (valid), right child 15 with left child 6 and right child 20.Step 2: Check BST property on right subtree
Node 6 is in right subtree of 10 but less than 10, violating BST property. So function returns false.Final Answer:
false -> Option CQuick Check:
6 in right subtree < 10 = false [OK]
- Ignoring min/max constraints in recursion
- Assuming all subtrees are BST without checks
- Confusing node values in subtrees
function isBST(root: TreeNode | null): boolean {
if (!root) return true;
if (root.left && root.left.val > root.val) return false;
if (root.right && root.right.val < root.val) return false;
return isBST(root.left) && isBST(root.right);
}Solution
Step 1: Understand the function checks
The function only compares immediate children values with current node, not entire subtree ranges.Step 2: Explain why this is a bug
BST requires all nodes in left subtree < current node and all in right subtree > current node, not just direct children.Final Answer:
It does not check the entire subtree values against min/max limits -> Option AQuick Check:
Missing min/max range check = It does not check the entire subtree values against min/max limits [OK]
- Only comparing direct children values
- Ignoring recursive min/max constraints
- Assuming null nodes cause errors
root.val <= min in the validation function:
function isBST(root: TreeNode | null, min: number = -Infinity, max: number = Infinity): boolean {
if (!root) return true;
if (root.val <= min || root.val >= max) return false;
return isBST(root.left, min, root.val) && isBST(root.right, root.val, max);
}Solution
Step 1: Understand duplicates on right subtree rule
Duplicates allowed only on right means left subtree nodes must be strictly less than current node, right subtree nodes greater than or equal.Step 2: Adjust min/max comparisons accordingly
Replaceroot.val <= minwithroot.val < min. This fails only if strictly less than min (allows equal to min on right recursion), keepingroot.val >= maxfor strict upper bound.Final Answer:
Change toroot.val < minto allow duplicates on right -> Option AQuick Check:
Duplicates right subtree = Change toroot.val < min[OK]
- Allowing duplicates on left subtree
- Using <= on both sides
- Not changing min/max checks
Ready to prove it?
Pass the test and this pattern moves unknown
