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 on a Simple Tree
What is the output of the following code that checks if a binary tree is a BST?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def is_bst(node, min_val=float('-inf'), max_val=float('inf')): if node is None: return True if not (min_val < node.val < max_val): return False return (is_bst(node.left, min_val, node.val) and is_bst(node.right, node.val, max_val)) root = Node(10) root.left = Node(5) root.right = Node(15) root.left.left = Node(2) root.left.right = Node(7) root.right.right = Node(20) print(is_bst(root))
Attempts:
2 left
💡 Hint
Check if all nodes follow the BST property: left < node < right.
✗ Incorrect
The tree satisfies BST properties at every node, so the function returns True.
❓ Predict Output
intermediateOutput of BST Validation on a Non-BST Tree
What is the output of the following code that checks if a binary tree is a BST?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def is_bst(node, min_val=float('-inf'), max_val=float('inf')): if node is None: return True if not (min_val < node.val < max_val): return False return (is_bst(node.left, min_val, node.val) and is_bst(node.right, node.val, max_val)) root = Node(10) root.left = Node(5) root.right = Node(8) # This violates BST property root.left.left = Node(2) root.left.right = Node(7) print(is_bst(root))
Attempts:
2 left
💡 Hint
Check if right child of root is greater than root.
✗ Incorrect
The right child of root has value 8 which is less than root 10, violating BST rules, so output is False.
🔧 Debug
advancedIdentify the Error in BST Validation Code
What error does the following code raise when checking if a tree is BST?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def is_bst(node, min_val=float('-inf'), max_val=float('inf')): if node is None: return True if not (min_val <= node.val <= max_val): # Incorrect comparison return False return (is_bst(node.left, min_val, node.val) and is_bst(node.right, node.val, max_val)) root = Node(10) root.left = Node(5) root.right = Node(10) # Duplicate value on right print(is_bst(root))
Attempts:
2 left
💡 Hint
BST usually does not allow duplicates on the right subtree.
✗ Incorrect
The code uses <= which allows duplicates on the right, violating strict BST rules. This is a logical error, not a runtime error.
🧠 Conceptual
advancedWhy Use Min and Max Values in BST Validation?
Why do we pass min_val and max_val parameters when validating if a binary tree is a BST?
Attempts:
2 left
💡 Hint
Think about how BST property applies to all descendants, not just immediate children.
✗ Incorrect
Passing min and max values helps ensure each node fits within the valid range defined by its ancestors, maintaining BST property globally.
🚀 Application
expertResult of Inorder Traversal After BST Validation
Given the following tree and BST validation code, what is the inorder traversal output if the tree is a BST?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def is_bst(node, min_val=float('-inf'), max_val=float('inf')): if node is None: return True if not (min_val < node.val < max_val): return False return (is_bst(node.left, min_val, node.val) and is_bst(node.right, node.val, max_val)) def inorder(node, result): if node: inorder(node.left, result) result.append(node.val) inorder(node.right, result) root = Node(8) root.left = Node(3) root.right = Node(10) root.left.left = Node(1) root.left.right = Node(6) root.right.right = Node(14) root.left.right.left = Node(4) root.left.right.right = Node(7) if is_bst(root): res = [] inorder(root, res) print(res) else: print("Not a BST")
Attempts:
2 left
💡 Hint
Inorder traversal of BST always gives sorted order of values.
✗ Incorrect
Inorder traversal visits nodes in ascending order for BSTs, so the output is the sorted list of node values.
