0
0
DSA C++programming~10 mins

Check if Binary Tree is Balanced in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Check if Binary Tree is Balanced
Start at root
Check left subtree height
Check right subtree height
Compare heights difference <= 1?
Yes
Check if left subtree balanced
Check if right subtree balanced
If both balanced, return True
No
Return False
We start from the root, check heights of left and right subtrees, compare their difference, and recursively verify if both subtrees are balanced.
Execution Sample
DSA C++
bool isBalanced(TreeNode* root) {
  if (!root) return true;
  int lh = height(root->left);
  int rh = height(root->right);
  if (abs(lh - rh) > 1) return false;
  return isBalanced(root->left) && isBalanced(root->right);
}
This code checks if a binary tree is balanced by comparing subtree heights and recursively checking subtrees.
Execution Table
StepOperationNode VisitedLeft HeightRight HeightHeight DifferenceBalanced?Tree State
1Start at root1????1 / \ 2 3
2Check left subtree height2????2 / \ 4 5
3Check left subtree height4000True4
4Check right subtree height5000True5
5Compare heights at node 22110True2 / \ 4 5
6Check right subtree height3????3 \ 6
7Check left subtree heightnull000Truenull
8Check right subtree height6000True6
9Compare heights at node 33011True3 \ 6
10Compare heights at root1220True1 / \ 2 3
11Check left subtree balanced2110True2 / \ 4 5
12Check right subtree balanced3011True3 \ 6
13Return final result1220True1 / \ 2 3
14ExitN/AN/AN/AN/ATrueBalanced tree confirmed
💡 All nodes checked, height differences <= 1, tree is balanced
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 9After Step 10Final
rootNode 1Node 1Node 1Node 1Node 1Node 1
lh (left height)?11022
rh (right height)???122
balanced?TrueTrueTrueTrueTrue
Key Moments - 3 Insights
Why do we check height difference at every node instead of only at the root?
Because a subtree can be unbalanced even if the root looks balanced. The execution_table rows 5, 9, and 10 show height differences checked at each node to ensure full balance.
What happens if a subtree is null (empty)?
A null subtree has height 0 and is considered balanced. Step 7 shows checking a null node returns height 0 and balanced True.
Why do we recursively check both left and right subtrees for balance?
Because both subtrees must be balanced for the whole tree to be balanced. Steps 11 and 12 show recursive balance checks on left and right subtrees.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the height difference at node 2?
A0
B1
C2
DUndefined
💡 Hint
Check the 'Height Difference' column at step 5 in the execution_table.
At which step does the algorithm confirm the right subtree of node 3 is balanced?
AStep 7
BStep 9
CStep 8
DStep 12
💡 Hint
Look at the 'Balanced?' column and 'Node Visited' at step 8 in the execution_table.
If the height difference at the root was 3, what would be the balanced result at step 10?
ATrue
BFalse
CDepends on subtrees
DCannot determine
💡 Hint
Recall the condition abs(lh - rh) > 1 returns false balance; see step 10 logic.
Concept Snapshot
Check if Binary Tree is Balanced:
- For each node, get 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
- Base case: null node is balanced with height 0
Full Transcript
To check if a binary tree is balanced, we start at the root node. We find the height of the left subtree and the right subtree. We compare their heights. If the difference is more than 1, the tree is not balanced. If the difference is 1 or less, we check recursively if the left subtree is balanced and if the right subtree is balanced. If both are balanced, the whole tree is balanced. If any subtree is not balanced, the whole tree is not balanced. A null subtree has height zero and is balanced by definition. This process continues until all nodes are checked or an imbalance is found.