0
0
DSA Typescriptprogramming~10 mins

Validate if Tree is BST in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Validate if Tree is BST
Start at root node
Check if node is null?
YesReturn True
No
Check node value > min and < max?
NoReturn False
Yes
Recurse left subtree with updated max = node value
Recurse right subtree with updated min = node value
Return left_result AND right_result
We start at the root and check if each node's value fits within allowed min and max limits, updating these limits as we go down left and right subtrees.
Execution Sample
DSA Typescript
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 against min and max bounds recursively.
Execution Table
StepOperationNode VisitedMin BoundMax BoundResultTree State
1Start at root10-InfinityInfinityContinue10 / \ 5 15
2Check node 10 value10-InfinityInfinity10 > -∞ and < ∞: True10 / \ 5 15
3Recurse left subtree5-Infinity10Continue10 / \ 5 15
4Check node 5 value5-Infinity105 > -∞ and < 10: True10 / \ 5 15
5Recurse left subtree2-Infinity5Continue10 / \ 5 15 / 2
6Check node 2 value2-Infinity52 > -∞ and < 5: True10 / \ 5 15 / 2
7Recurse left subtreenull-Infinity2True (null node)10 / \ 5 15 / 2
8Recurse right subtreenull25True (null node)10 / \ 5 15 / 2
9Left subtree of 5 is BST5-Infinity10True10 / \ 5 15 / 2
10Recurse right subtree7510Continue10 / \ 5 15 / \ 2 7
11Check node 7 value75107 > 5 and < 10: True10 / \ 5 15 / \ 2 7
12Recurse left subtreenull57True (null node)10 / \ 5 15 / \ 2 7
13Recurse right subtreenull710True (null node)10 / \ 5 15 / \ 2 7
14Right subtree of 5 is BST5-Infinity10True10 / \ 5 15 / \ 2 7
15Left subtree of 10 is BST10-InfinityInfinityTrue10 / \ 5 15 / \ 2 7
16Recurse right subtree1510InfinityContinue10 / \ 5 15 / \ 2 7 20
17Check node 15 value1510Infinity15 > 10 and < ∞: True10 / \ 5 15 / \ 2 7 20
18Recurse left subtreenull1015True (null node)10 / \ 5 15 / \ 2 7 20
19Recurse right subtree2015InfinityContinue10 / \ 5 15 / \ 2 7 20
20Check node 20 value2015Infinity20 > 15 and < ∞: True10 / \ 5 15 / \ 2 7 20
21Recurse left subtreenull1520True (null node)10 / \ 5 15 / \ 2 7 20
22Recurse right subtreenull20InfinityTrue (null node)10 / \ 5 15 / \ 2 7 20
23Right subtree of 15 is BST1510InfinityTrue10 / \ 5 15 / \ 2 7 20
24Right subtree of 10 is BST10-InfinityInfinityTrue10 / \ 5 15 / \ 2 7 20
25Final resultroot-InfinityInfinityTrue (Tree is BST)10 / \ 5 15 / \ 2 7 20
💡 All nodes satisfy BST conditions; recursion ends with True.
Variable Tracker
VariableStartAfter Step 3After Step 9After Step 15After Step 24Final
node10 (root)5 (left child)5 (left child)10 (root)15 (right child)10 (root)
min-Infinity-Infinity-Infinity-Infinity10-Infinity
maxInfinity1010InfinityInfinityInfinity
resultN/AContinueTrue (left subtree BST)True (left subtree BST)True (right subtree BST)True (whole tree BST)
Key Moments - 3 Insights
Why do we update max when going left and min when going right?
Because left subtree nodes must be less than the current node (max updated to node.val), and right subtree nodes must be greater (min updated to node.val). See execution_table steps 3 and 16.
What happens when we reach a null node?
We return True because an empty subtree is always a valid BST. This is shown in steps 7, 8, 12, 13, 18, 21, and 22.
Why do we check node.val <= min or >= max to return False?
Because if node value is not strictly between min and max, it violates BST rules. This check ensures no duplicates or invalid placements. See step 2 for the condition.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 10, what is the min and max bounds for node 7?
Amin = 7, max = 10
Bmin = 5, max = 10
Cmin = -Infinity, max = 5
Dmin = 10, max = Infinity
💡 Hint
Check the 'Min Bound' and 'Max Bound' columns at step 10 in execution_table.
At which step does the recursion confirm the left subtree of node 5 is a BST?
AStep 9
BStep 14
CStep 15
DStep 24
💡 Hint
Look for the row where 'Left subtree of 5 is BST' is mentioned in the 'Operation' column.
If node 7 had value 11, what would happen in the execution_table?
AStep 10 min and max bounds would change
BStep 11 would continue as normal
CStep 11 would return False because 11 is not < 10
DThe tree would still be a BST
💡 Hint
Check the condition at step 11 where node value must be between min and max.
Concept Snapshot
Validate if Tree is BST:
- Start at root with min = -∞, max = ∞
- For each node, check if min < node.val < max
- Recurse left with max = node.val
- Recurse right with min = node.val
- Return True if all nodes satisfy conditions
- Return False immediately if violation found
Full Transcript
To check if a binary tree is a BST, we start at the root node with minimum and maximum bounds set to negative and positive infinity. For each node, we verify if its value is strictly greater than the min bound and less than the max bound. We then recursively check the left subtree, updating the max bound to the current node's value, and the right subtree, updating the min bound to the current node's value. If any node violates these bounds, we return false immediately. If we reach a null node, we return true since an empty subtree is valid. The process continues until all nodes are validated, confirming if the tree is a BST or not.