0
0
DSA Typescriptprogramming~10 mins

Check if Binary Tree is Balanced in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Check if Binary Tree is Balanced
Start at root node
Check left subtree height
Check right subtree height
Compare heights difference <= 1?
Yes No
Check left balanced?
Check right balanced?
Return true if both balanced
The process checks heights of left and right subtrees at each node, ensuring difference is at most 1 and both subtrees are balanced.
Execution Sample
DSA Typescript
function isBalanced(root: TreeNode | null): boolean {
  function height(node: TreeNode | null): number {
    if (!node) return 0;
    const left = height(node.left);
    const right = height(node.right);
    if (left === -1 || right === -1 || Math.abs(left - right) > 1) return -1;
    return Math.max(left, right) + 1;
  }
  return height(root) !== -1;
}
This code checks if a binary tree is balanced by recursively computing subtree heights and returning -1 if imbalance is found.
Execution Table
StepOperationNode VisitedLeft HeightRight HeightHeight ReturnedBalanced StatusTree State
1Start at rootA---UnknownA ├─ B │ ├─ D │ └─ E └─ C
2Check left subtreeB---UnknownSame as above
3Check left subtreeD---UnknownSame as above
4Left child nullnull000BalancedSame as above
5Right child nullnull000BalancedSame as above
6Calculate height DD001BalancedSame as above
7Check right subtreeE---UnknownSame as above
8Left child nullnull000BalancedSame as above
9Right child nullnull000BalancedSame as above
10Calculate height EE001BalancedSame as above
11Calculate height BB112BalancedSame as above
12Check right subtreeC---UnknownSame as above
13Left child nullnull000BalancedSame as above
14Right child nullnull000BalancedSame as above
15Calculate height CC001BalancedSame as above
16Calculate height AA213BalancedSame as above
17Return final resultA213BalancedTree is balanced
💡 Traversal ends after checking all nodes; no imbalance found, so tree is balanced.
Variable Tracker
VariableStartAfter Step 6After Step 11After Step 15After Step 16Final
nodeADBCAnull
left height-0102-
right height-0101-
height returned-1213-
balanced statusUnknownBalancedBalancedBalancedBalancedBalanced
Key Moments - 3 Insights
Why do we return -1 when imbalance is detected?
Returning -1 signals imbalance immediately, stopping further checks. See steps 6 and 16 where heights are compared; if difference >1, -1 is returned to propagate failure.
Why do we check left and right subtree heights before deciding balanced status?
Because balance depends on both subtrees being balanced and their heights differing by at most 1. Steps 4-10 show recursive height checks before final decision at step 16.
What happens if a node is null?
Null nodes return height 0 (steps 4,5,8,9,13,14), which is the base case for recursion and means empty subtree is balanced.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 11, what is the height returned for node B?
A1
B2
C3
D-1
💡 Hint
Check the 'Height Returned' column at step 11 in the execution_table.
At which step does the algorithm confirm the tree is balanced?
AStep 6
BStep 11
CStep 16
DStep 17
💡 Hint
Look at the 'Balanced Status' and 'Return final result' in the execution_table.
If the right subtree of node A had height 0 instead of 1, what would be the height returned at step 16?
A2
B3
C-1
D1
💡 Hint
Height at step 16 is max(left height, right height) + 1; check variable_tracker for heights.
Concept Snapshot
Check if Binary Tree is Balanced:
- Recursively find height of left and right subtrees
- If height difference > 1, return -1 (unbalanced)
- If any subtree returns -1, propagate -1 up
- If root returns -1, tree is unbalanced
- Otherwise, tree is balanced
Full Transcript
This visualization shows how to check if a binary tree is balanced by recursively computing the height of each subtree. Starting from the root, the algorithm checks left and right subtree heights. If the difference is more than 1, it returns -1 to indicate imbalance. Null nodes return height 0. The process continues until all nodes are checked or imbalance is found. The execution table tracks each node visited, heights computed, and balanced status. The variable tracker shows how heights and nodes change during recursion. Key moments clarify why -1 is used and how null nodes are handled. The quiz tests understanding of height values and final balanced result.