0
0
DSA C++programming~10 mins

Validate if Tree is BST in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Validate if Tree is BST
Start at root node
Check left subtree
Is left subtree BST?
Check current node value
Check right subtree
Is right subtree BST?
Return True
We check recursively if left subtree is BST, then current node value fits BST rules, then right subtree is BST.
Execution Sample
DSA C++
bool isBST(Node* node, int min, int max) {
  if (!node) return true;
  if (node->val <= min || node->val >= max) return false;
  return isBST(node->left, min, node->val) && isBST(node->right, node->val, max);
}
This code checks if a binary tree is a BST by verifying node values lie within valid min and max ranges.
Execution Table
StepOperationNode VisitedMin AllowedMax AllowedCheck ResultReturn ValueTree State
1Start at root10-∞+∞10 > -∞ and 10 < +∞Pending[10]
2Check left subtree5-∞105 > -∞ and 5 < 10Pending[10, 5]
3Check left subtree of 52-∞52 > -∞ and 2 < 5Pending[10, 5, 2]
4Check left subtree of 2null-∞2null nodeTrue[10, 5, 2]
5Check right subtree of 2null25null nodeTrue[10, 5, 2]
6Return from node 22-∞5Both subtrees TrueTrue[10, 5, 2]
7Check right subtree of 575107 > 5 and 7 < 10Pending[10, 5, 2, 7]
8Check left subtree of 7null57null nodeTrue[10, 5, 2, 7]
9Check right subtree of 7null710null nodeTrue[10, 5, 2, 7]
10Return from node 77510Both subtrees TrueTrue[10, 5, 2, 7]
11Return from node 55-∞10Both subtrees TrueTrue[10, 5, 2, 7]
12Check right subtree1510+∞15 > 10 and 15 < +∞Pending[10, 5, 2, 7, 15]
13Check left subtree of 1512101512 > 10 and 12 < 15Pending[10, 5, 2, 7, 15, 12]
14Check left subtree of 12null1012null nodeTrue[10, 5, 2, 7, 15, 12]
15Check right subtree of 12null1215null nodeTrue[10, 5, 2, 7, 15, 12]
16Return from node 12121015Both subtrees TrueTrue[10, 5, 2, 7, 15, 12]
17Check right subtree of 152015+∞20 > 15 and 20 < +∞Pending[10, 5, 2, 7, 15, 12, 20]
18Check left subtree of 20null1520null nodeTrue[10, 5, 2, 7, 15, 12, 20]
19Check right subtree of 20null20+∞null nodeTrue[10, 5, 2, 7, 15, 12, 20]
20Return from node 202015+∞Both subtrees TrueTrue[10, 5, 2, 7, 15, 12, 20]
21Return from node 151510+∞Both subtrees TrueTrue[10, 5, 2, 7, 15, 12, 20]
22Return from root 1010-∞+∞Both subtrees TrueTrue[10, 5, 2, 7, 15, 12, 20]
💡 All nodes satisfy BST conditions, recursion ends with True
Variable Tracker
VariableStartAfter Step 2After Step 6After Step 11After Step 21Final
node10 (root)5 (left child)5 (left child)10 (root)15 (right child)10 (root)
min-∞-∞-∞-∞10-∞
max+∞101010+∞+∞
return valuePendingPendingTrueTrueTrueTrue
Key Moments - 3 Insights
Why do we pass min and max values when checking each node?
The min and max track the allowed value range for each node. See execution_table steps 2, 12 where min and max change to keep BST rules.
What happens when we reach a null node?
Null nodes mean leaf ends and are considered valid BST parts. See steps 4, 5, 8 where null nodes return True.
Why do we check node->val <= min or >= max instead of just equality?
BST requires strict less or greater, no duplicates allowed. See step 3 where node value must be strictly between min and max.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what is the min and max allowed for node 7?
Amin = 5, max = 10
Bmin = -∞, max = 10
Cmin = 2, max = 7
Dmin = 7, max = 10
💡 Hint
Check columns 'Min Allowed' and 'Max Allowed' at step 7 in execution_table
At which step does the recursion confirm the left subtree of root is a BST?
AStep 6
BStep 11
CStep 21
DStep 22
💡 Hint
Look for 'Return from node 5' with return value True in execution_table
If node 12 had value 9 instead, which step would fail the BST check?
AStep 16
BStep 12
CStep 13
DStep 17
💡 Hint
Check step 13 where node 12 is checked against min=10 and max=15
Concept Snapshot
Validate BST by recursion:
- Start at root with min=-∞, max=+∞
- Check node value in (min, max)
- Recurse left with max=node.val
- Recurse right with min=node.val
- Null nodes return True
- If any check fails, return False
Full Transcript
To check if a binary tree is a BST, we start at the root node and verify if its value lies within allowed minimum and maximum bounds. Initially, these bounds are negative and positive infinity. We then recursively check the left subtree, updating the maximum bound to the current node's value, and the right subtree, updating the minimum bound to the current node's value. If a node is null, it is considered valid. If any node violates the bounds, the tree is not a BST. The execution table shows each step visiting nodes, checking values against bounds, and returning True or False accordingly. The variable tracker follows the current node and bounds during recursion. Key moments clarify why bounds are needed, how null nodes are handled, and why strict inequalities are used. The visual quiz tests understanding of bounds at specific steps and failure points. This method ensures the entire tree satisfies BST properties.