0
0
DSA Goprogramming~10 mins

Validate if Tree is BST in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - Validate if Tree is BST
Start at root node
Check left subtree values < current node?
Recursively validate left subtree
Check right subtree values > current node?
Recursively validate right subtree
If all checks pass, return True
Else return False
We start at the root and recursively check if all nodes in the left subtree are smaller and all nodes in the right subtree are larger, ensuring BST properties hold.
Execution Sample
DSA Go
func isBST(node *Node, min, max int) bool {
  if node == nil {
    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 function checks if a binary tree is a BST by ensuring each node's value is within valid min and max bounds.
Execution Table
StepOperationNode VisitedMin BoundMax BoundResultTree State
1Start at root10-∞+∞Check node 1010 / \ 5 15
2Check left subtree5-∞10Check node 510 / \ 5 15
3Check left of 53-∞5Check node 310 / \ 5 15 / 3
4Left of 3 is nilnil-∞3TrueNo change
5Right of 3 is nilnil35TrueNo change
6Left subtree of 5 valid5-∞10TrueNo change
7Check right subtree7510Check node 710 / \ 5 15 \ 7
8Left of 7 is nilnil57TrueNo change
9Right of 7 is nilnil710TrueNo change
10Right subtree of 5 valid7510TrueNo change
11Left subtree of 10 valid5-∞10TrueNo change
12Check right subtree1510+∞Check node 1510 / \ 5 15
13Left of 15 is nilnil1015TrueNo change
14Right of 15 is nilnil15+∞TrueNo change
15Right subtree of 10 valid1510+∞TrueNo change
16All checks passed10-∞+∞TrueTree is BST
💡 All nodes satisfy BST property within min and max bounds, so tree is BST
Variable Tracker
VariableStartAfter Step 2After Step 6After Step 11After Step 16
node10 (root)5 (left child)5 (left child)10 (root)10 (root)
min-∞-∞-∞-∞-∞
max+∞1010+∞+∞
resultN/ACheckingTrueTrueTrue
Key Moments - 3 Insights
Why do we pass min and max bounds when checking each node?
Because each node must be greater than all nodes in its left subtree (min bound) and less than all nodes in its right subtree (max bound). This is shown in execution_table steps 3, 7, and 12 where bounds narrow.
What happens when we reach a nil (empty) node?
We return True immediately because an empty subtree is always valid. This is shown in steps 4, 5, 8, 9, 13, and 14.
Why do we check node.val <= min or node.val >= max?
To ensure the current node's value fits strictly between allowed bounds. If it doesn't, we return False immediately, stopping further checks. This is the core BST validation condition.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what are the min and max bounds for node 3?
A-∞ and 5
B-∞ and 10
C5 and 10
D3 and 5
💡 Hint
Check the 'Min Bound' and 'Max Bound' columns at step 3 in execution_table
At which step does the function confirm the left subtree of node 5 is valid?
AStep 4
BStep 6
CStep 9
DStep 11
💡 Hint
Look for the row where 'Left subtree of 5 valid' is noted in the 'Operation' column
If node 7 had value 11 instead, which step would fail the BST check?
AStep 7
BStep 10
CStep 12
DStep 16
💡 Hint
Check the 'Result' column at step 10 where node 7 is validated against bounds 5 and 10
Concept Snapshot
Validate BST by recursively checking each node's value is > min and < max bounds.
Pass updated bounds down the tree.
Return true if all nodes satisfy conditions.
Return false immediately if any node violates bounds.
Nil nodes are valid by default.
Use recursion with min/max parameters.
Full Transcript
To check if a binary tree is a BST, we start at the root node and recursively verify that all nodes in the left subtree have values less than the current node, and all nodes in the right subtree have values greater than the current node. We keep track of minimum and maximum allowed values for each node. If a node's value is outside these bounds, we return false immediately. If we reach a nil node, we return true because an empty subtree is valid. This process continues until all nodes are checked or a violation is found. The example tree with root 10, left child 5, and right child 15 passes all checks, confirming it is a BST.