0
0
DSA Javascriptprogramming~10 mins

Check if Binary Tree is Balanced in DSA Javascript - 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
Start from the root, check heights of left and right subtrees, verify difference ≤ 1, then check if both subtrees are balanced.
Execution Sample
DSA Javascript
function isBalanced(root) {
  if (!root) return true;
  function height(node) {
    if (!node) return 0;
    return 1 + Math.max(height(node.left), height(node.right));
  }
  const leftHeight = height(root.left);
  const rightHeight = height(root.right);
  if (Math.abs(leftHeight - rightHeight) > 1) return false;
  return isBalanced(root.left) && isBalanced(root.right);
}
Checks if a binary tree is balanced by comparing subtree heights and recursively verifying balance.
Execution Table
StepOperationNode VisitedLeft HeightRight HeightHeight DifferenceBalanced?Visual State
1Start at rootA????A / \ B C
2Calculate left subtree heightB11??A / \ B C / \ D E
3Calculate right subtree heightC00??A / \ B C / \ D E
4Compare heights at AA211YesA / \ B C / \ D E
5Check if left subtree balancedB110YesA / \ B C / \ D E
6Check if right subtree balancedC000YesA / \ B C / \ D E
7Return final resultA211TrueA / \ B C / \ D E
💡 All nodes checked, height differences ≤ 1, tree is balanced
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
currentNodeABCABCA
leftHeight?112222
rightHeight??01101
heightDifference???1001
balanced???YesYesYesTrue
Key Moments - 3 Insights
Why do we calculate height for both left and right subtrees before checking balance?
Because the balance depends on the height difference between left and right subtrees (see steps 2, 3, and 4 in execution_table). We need both heights to compare.
Why do we recursively check if left and right subtrees are balanced after comparing heights?
Because even if the current node's left and right subtree heights differ by ≤1, their subtrees might be unbalanced. Steps 5 and 6 show these recursive checks.
What does it mean if height difference is greater than 1 at any node?
It means the tree is not balanced at that node, so the function returns false immediately (see step 4 where difference >1 would cause 'Balanced?' to be No).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the height difference at node A?
A2
B0
C1
D3
💡 Hint
Check the 'Height Difference' column at step 4 in execution_table
At which step do we confirm that the right subtree rooted at C is balanced?
AStep 3
BStep 6
CStep 4
DStep 7
💡 Hint
Look at the 'Balanced?' column and 'Node Visited' at step 6 in execution_table
If the left subtree height at node B was 3 instead of 1, what would happen at step 4?
AHeight difference would be greater than 1, so balanced would be false
BHeight difference would still be 1, balanced true
CThe function would skip checking right subtree
DThe function would return true immediately
💡 Hint
Height difference is absolute difference of left and right heights; if left is 3 and right is 1, difference is 2 > 1
Concept Snapshot
Check if Binary Tree is Balanced:
- For each node, find left and right subtree heights
- If height difference > 1, tree is unbalanced
- Recursively check left and right subtrees
- Return true only if all nodes balanced
- Uses post-order traversal to compute heights
Full Transcript
This visualization shows how to check if a binary tree is balanced. We start at the root node and calculate the height of its left and right subtrees. The height is the longest path from a node down to a leaf. We compare these heights; if their difference is more than 1, the tree is not balanced. Then we recursively check if the left and right subtrees themselves are balanced. The execution table tracks each step, showing the node visited, heights calculated, and balance status. The variable tracker shows how key variables change during execution. Key moments clarify why we calculate heights first and why recursive checks are needed. The quiz tests understanding of height differences and balance checks. This method uses a post-order style traversal to ensure all nodes are checked for balance.