0
0
DSA Javascriptprogramming~10 mins

Validate if Tree is BST in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Validate if Tree is BST
Start at root node
Check node value within allowed range?
Yes
Recurse left subtree with updated max bound
Recurse right subtree with updated min bound
If all nodes valid, return True
Else return False
Start from the root, check if each node's value fits the BST rules within allowed min and max bounds, recursively check left and right subtrees.
Execution Sample
DSA Javascript
function isBST(node, min = -Infinity, max = Infinity) {
  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 function checks if a binary tree is a BST by verifying node values are within valid min and max bounds recursively.
Execution Table
StepOperationNode VisitedAllowed Range (min, max)ResultTree State
1Start at root10(-∞, ∞)Check 10 in range10 / \ 5 15
2Recurse left5(-∞, 10)Check 5 in range10 / \ 5 15
3Recurse left.left3(-∞, 5)Check 3 in range10 / \ 5 15 / 3
4Recurse left.left.leftnull(-∞, 3)Null node, return trueNo change
5Recurse left.left.rightnull(3, 5)Null node, return trueNo change
6Back to 5, recurse right7(5, 10)Check 7 in range10 / \ 5 15 / \ 3 7
7Recurse right.leftnull(5, 7)Null node, return trueNo change
8Recurse right.rightnull(7, 10)Null node, return trueNo change
9Back to root, recurse right15(10, ∞)Check 15 in range10 / \ 5 15 / \ 3 7
10Recurse right.left13(10, 15)Check 13 in range10 / \ 5 15 / \ / 3 7 13
11Recurse right.left.leftnull(10, 13)Null node, return trueNo change
12Recurse right.left.rightnull(13, 15)Null node, return trueNo change
13Recurse right.right20(15, ∞)Check 20 in range10 / \ 5 15 / \ / \ 3 7 13 20
14Recurse right.right.leftnull(15, 20)Null node, return trueNo change
15Recurse right.right.rightnull(20, ∞)Null node, return trueNo change
16All nodes validN/AN/AReturn trueTree is BST
💡 All nodes satisfy BST property within their allowed ranges, so the tree is a valid BST.
Variable Tracker
VariableStartAfter Step 2After Step 6After Step 9After Step 13Final
node.val10571520N/A
min-∞-∞51015N/A
max1010N/A
Return valueN/Atruetruetruetruetrue
Key Moments - 3 Insights
Why do we update the max bound when going left and min bound when going right?
Because in a BST, all left subtree nodes must be less than the current node, so max is current node's value; all right subtree nodes must be greater, so min is current node's value. See steps 2 and 9 in execution_table.
What happens when we reach a null node?
A null node means leaf's child, which is valid by default, so we return true. See steps 4, 5, 7, 8, 11, 12, 14, 15.
Why do we check node.val <= min or node.val >= max instead of just equality?
Because BST requires strictly less or greater values, duplicates are not allowed on either side. This ensures strict ordering. See step 3 where node.val is checked against min and max.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is the allowed range for node 7?
A(5, 10)
B(-∞, 10)
C(5, 7)
D(7, 10)
💡 Hint
Check the 'Allowed Range' column at step 6 in execution_table.
At which step does the recursion check the right child of node 15?
AStep 10
BStep 9
CStep 13
DStep 16
💡 Hint
Look for 'Recurse right.right' operation in execution_table.
If node 13 had value 16, which step would fail the BST check?
AStep 10
BStep 13
CStep 6
DStep 2
💡 Hint
Check the allowed range for node 13 at step 10 and what happens at step 13.
Concept Snapshot
Validate BST by checking each node's value is strictly between min and max bounds.
Start at root with (-∞, ∞).
Recurse left with max = node.val.
Recurse right with min = node.val.
Return false if any node violates bounds.
Return true if all nodes valid.
Full Transcript
To check if a binary tree is a BST, we start at the root node and verify if its value lies within an allowed range, initially from negative infinity to positive infinity. For each node, we recursively check the left subtree with an updated maximum bound equal to the current node's value, and the right subtree with an updated minimum bound equal to the current node's value. If any node's value is not strictly within these bounds, the tree is not a BST. Null nodes are considered valid leaves. This process continues until all nodes are validated or a violation is found. The example tree with nodes 10, 5, 15, 3, 7, 13, and 20 passes all checks and is a valid BST.