Challenge - 5 Problems
BST Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of BST Validation for a Simple Tree
What is the output of the following Java code that checks if a binary tree is a BST?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } public class ValidateBST { static boolean isBST(Node root, Integer min, Integer max) { if (root == null) return true; if ((min != null && root.val <= min) || (max != null && root.val >= max)) return false; return isBST(root.left, min, root.val) && isBST(root.right, root.val, max); } public static void main(String[] args) { Node root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(2); root.left.right = new Node(7); root.right.left = new Node(12); root.right.right = new Node(20); System.out.println(isBST(root, null, null)); } }
Attempts:
2 left
💡 Hint
Check if all nodes follow BST rules: left < root < right
✗ Incorrect
The tree satisfies BST properties because every node's left subtree contains smaller values and right subtree contains larger values.
❓ Predict Output
intermediateOutput for Tree with Invalid BST Node
What will be the output of this Java code that validates if a binary tree is a BST?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } public class ValidateBST { static boolean isBST(Node root, Integer min, Integer max) { if (root == null) return true; if ((min != null && root.val <= min) || (max != null && root.val >= max)) return false; return isBST(root.left, min, root.val) && isBST(root.right, root.val, max); } public static void main(String[] args) { Node root = new Node(10); root.left = new Node(5); root.right = new Node(8); // Invalid: 8 < 10 but on right System.out.println(isBST(root, null, null)); } }
Attempts:
2 left
💡 Hint
Check if right child is greater than root
✗ Incorrect
The right child 8 is less than root 10, violating BST property. So output is false.
🔧 Debug
advancedIdentify the Error in BST Validation Code
What error will this Java code produce when checking if a tree is a BST?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } public class ValidateBST { static boolean isBST(Node root, int min, int max) { if (root == null) return true; if (root.val <= min || root.val >= max) return false; return isBST(root.left, min, root.val) && isBST(root.right, root.val, max); } public static void main(String[] args) { Node root = new Node(10); root.left = new Node(5); root.right = new Node(15); System.out.println(isBST(root, null, null)); } }
Attempts:
2 left
💡 Hint
Check parameter types and arguments passed
✗ Incorrect
The method expects int parameters but null is passed, causing a compilation error.
🚀 Application
advancedNumber of Nodes in a Valid BST
Given the following tree, how many nodes does the tree have if it is a valid BST?
DSA Java
class Node { int val; Node left, right; Node(int val) { this.val = val; } } // Tree structure: // 8 // / \ // 3 10 // / \ \ // 1 6 14 // / \ / // 4 7 13 public class CountNodes { static int countNodes(Node root) { if (root == null) return 0; return 1 + countNodes(root.left) + countNodes(root.right); } public static void main(String[] args) { Node root = new Node(8); root.left = new Node(3); root.right = new Node(10); root.left.left = new Node(1); root.left.right = new Node(6); root.left.right.left = new Node(4); root.left.right.right = new Node(7); root.right.right = new Node(14); root.right.right.left = new Node(13); System.out.println(countNodes(root)); } }
Attempts:
2 left
💡 Hint
Count all nodes in the tree
✗ Incorrect
The tree has 9 nodes total as counted by the recursive function.
🧠 Conceptual
expertWhy Use Min and Max Bounds in BST Validation?
Why does the BST validation algorithm use min and max bounds when checking each node?
Attempts:
2 left
💡 Hint
Think about how BST property applies to all descendants, not just immediate children
✗ Incorrect
Min and max bounds track the allowed value range for each node based on its ancestors, ensuring the entire subtree respects BST rules.
