Discover how a simple rule can save you hours of confusing tree checks!
Why Validate if Tree is BST in DSA C#?
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 and comparing with all others is tiring and confusing.
Manually checking each node against all others takes a lot of time and can easily lead to mistakes. You might forget to check some parts or mix up the order, making the whole process slow and error-prone.
Using a smart method to check the tree step-by-step, comparing nodes only with allowed minimum and maximum values, makes the process fast and reliable. This method quickly tells if the tree follows the special rule or not.
bool IsBST(Node root) {
if (root == null) return true;
if (!CheckAllNodes(root.left, root.value, true)) return false;
if (!CheckAllNodes(root.right, root.value, false)) return false;
return IsBST(root.left) && IsBST(root.right);
}bool IsBST(Node node, int? min, int? max) {
if (node == null) return true;
if ((min != null && node.value <= min.Value) || (max != null && node.value >= max.Value)) return false;
return IsBST(node.left, min, node.value) && IsBST(node.right, node.value, max);
}This method enables quick and accurate verification of whether a tree follows the binary search tree rules, making complex tree checks simple and error-free.
When building a phone contact list that must be sorted for fast search, verifying the structure is a valid binary search tree ensures quick lookups and smooth user experience.
Manual checking of tree order is slow and error-prone.
Using min and max limits for each node simplifies validation.
This method ensures fast and reliable BST verification.
Practice
Solution
Step 1: Understand BST property
A BST requires that for every node, values in the left subtree are smaller and values in the right subtree are larger.Step 2: Compare options with BST definition
For each node, all nodes in the left subtree are smaller and all nodes in the right subtree are larger matches the BST property exactly, while others describe different tree properties.Final Answer:
For each node, all nodes in the left subtree are smaller and all nodes in the right subtree are larger -> Option AQuick Check:
BST property = For each node, all nodes in the left subtree are smaller and all nodes in the right subtree are larger [OK]
- Confusing BST with balanced tree
- Thinking all nodes must have two children
- Ignoring the ordering property
Solution
Step 1: Identify parameters needed for BST validation
To validate BST, we need to check node values within a range (min, max) as we traverse.Step 2: Match method signature with required parameters
bool IsBST(Node root, int min, int max) includes root node and min/max range, suitable for recursive BST check.Final Answer:
bool IsBST(Node root, int min, int max) -> Option AQuick Check:
Method with min/max range = bool IsBST(Node root, int min, int max) [OK]
- Using void return type instead of bool
- Missing min and max parameters for range checks
- Passing parent node instead of value ranges
class Node {
public int val;
public Node left, right;
public Node(int v) { val = v; left = right = null; }
}
bool IsBST(Node node, int min, int max) {
if (node == null) return true;
if (node.val <= min || node.val >= max) return false;
return IsBST(node.left, min, node.val) && IsBST(node.right, node.val, max);
}
var root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(1);
root.left.right = new Node(12);
Console.WriteLine(IsBST(root, int.MinValue, int.MaxValue));Solution
Step 1: Trace the tree structure and BST property
The tree has root 10, left child 5, right child 15. Left child's right child is 12, which is greater than 10 but in left subtree.Step 2: Check BST validation logic
IsBST will fail because 12 is in left subtree of 10 but greater than 10, violating BST rules.Final Answer:
False -> Option BQuick Check:
Node 12 in left subtree breaks BST = False [OK]
- Assuming all nodes are valid without checking subtree ranges
- Ignoring node 12's position violating BST
- Confusing left and right subtree values
bool IsBST(Node node) {
if (node == null) return true;
if (node.left != null && node.left.val > node.val) return false;
if (node.right != null && node.right.val < node.val) return false;
return IsBST(node.left) && IsBST(node.right);
}Solution
Step 1: Analyze the checks performed
The code only compares node values with immediate left and right children, not entire subtrees.Step 2: Understand BST validation requirement
BST requires all nodes in left subtree < node, and all in right subtree > node, not just immediate children.Final Answer:
It does not check the entire subtree values, only immediate children -> Option DQuick Check:
Check full subtree values, not just children [OK]
- Only comparing immediate children values
- Ignoring subtree min/max constraints
- Assuming null handling is incorrect
Solution
Step 1: Understand duplicate placement rule
Duplicates allowed only on right subtree means left subtree nodes must be strictly less than node, right subtree nodes can be equal or greater.Step 2: Adjust comparison operators accordingly
Left subtree: node.val > min (strictly less), right subtree: node.val <= max (allow equal or greater).Final Answer:
Allow duplicates only if node.val > min for left subtree and node.val <= max for right subtree -> Option CQuick Check:
Duplicates on right subtree use <= max [OK]
- Allowing duplicates on left subtree
- Using <= on left subtree incorrectly
- Disallowing duplicates completely when allowed
Ready to prove it?
Pass the test and this pattern moves unknown
