0
0
DSA C++programming~10 mins

Diameter of Binary Tree in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Diameter of Binary Tree
Start at root node
Compute left subtree height
Compute right subtree height
Calculate diameter through current node = left height + right height
Update max diameter if current diameter is larger
Return height of current node = max(left height, right height) + 1
Repeat for left and right child nodes recursively
Final max diameter stored after full traversal
End
The diameter is found by recursively computing heights of left and right subtrees at each node, updating the maximum diameter found as the sum of these heights.
Execution Sample
DSA C++
int diameter = 0;
int height(TreeNode* node) {
  if (!node) return 0;
  int left = height(node->left);
  int right = height(node->right);
  diameter = max(diameter, left + right);
  return max(left, right) + 1;
}
This code recursively calculates the height of each subtree and updates the diameter as the sum of left and right subtree heights at each node.
Execution Table
StepOperationNode VisitedLeft HeightRight HeightDiameter UpdateDiameter ValueVisual State
1Visit root1??Calculate left and right heights0Root(1)
2Visit left child2??Calculate left and right heights0Root(1) -> Left(2)
3Visit left-left child400Diameter = max(0,0+0)=00Root(1) -> Left(2) -> Left(4)
4Return height from node 4400--Height=1 at node 4
5Visit left-right child500Diameter = max(0,0+0)=00Root(1) -> Left(2) -> Right(5)
6Return height from node 5500--Height=1 at node 5
7Calculate height at node 2211Diameter = max(0,1+1)=22Height=2 at node 2
8Visit right child3??Calculate left and right heights2Root(1) -> Right(3)
9Visit right-left child600Diameter = max(2,0+0)=22Root(1) -> Right(3) -> Left(6)
10Return height from node 6600--Height=1 at node 6
11Visit right-right child700Diameter = max(2,0+0)=22Root(1) -> Right(3) -> Right(7)
12Return height from node 7700--Height=1 at node 7
13Calculate height at node 3311Diameter = max(2,1+1)=22Height=2 at node 3
14Calculate height at root node 1122Diameter = max(2,2+2)=44Height=3 at root
15End----4Final diameter = 4
💡 All nodes visited, diameter updated to maximum sum of left and right subtree heights.
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 7After Step 10After Step 13After Step 14Final
diameter00022244
height(node 4)?1111111
height(node 5)??111111
height(node 2)???22222
height(node 6)????1111
height(node 7)????1111
height(node 3)?????222
height(root 1)??????33
Key Moments - 3 Insights
Why do we add left and right subtree heights to update the diameter?
Because the diameter is the longest path between two nodes, which passes through the current node as the sum of left and right subtree heights (see Step 7 and Step 14 in execution_table).
Why do we return max(left height, right height) + 1 as the height?
Height is the longest path from current node down to a leaf, so we take the larger subtree height and add 1 for the current node (see Steps 4, 6, 7, 13, 14).
Why does the diameter update happen before returning height?
Because diameter depends on both left and right subtree heights at the current node, so we must compute and update diameter before returning height (see Steps 7 and 14).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 7, what is the diameter value after visiting node 2?
A1
B2
C0
D4
💡 Hint
Check the 'Diameter Value' column at Step 7 in execution_table.
At which step does the diameter first update to 4?
AStep 13
BStep 7
CStep 14
DStep 15
💡 Hint
Look at the 'Diameter Update' and 'Diameter Value' columns in execution_table for the largest update.
If the left subtree of root had height 1 instead of 2, how would the final diameter change?
AIt would decrease by 1
BIt would remain 4
CIt would increase by 1
DIt would become 2
💡 Hint
Diameter is sum of left and right heights at root (see Step 14 in execution_table).
Concept Snapshot
Diameter of Binary Tree:
- Diameter = longest path between any two nodes
- At each node, diameter = left height + right height
- Use recursion to get heights of left and right subtrees
- Update max diameter during traversal
- Return height = max(left, right) + 1
- Final diameter stored in global variable
Full Transcript
The diameter of a binary tree is the longest path between any two nodes. We find it by visiting each node and calculating the height of its left and right subtrees. At each node, we add these heights to get the diameter passing through that node. We keep track of the maximum diameter found so far. The height returned from each node is one plus the maximum height of its subtrees. This process is done recursively starting from the root. The final diameter is the maximum sum of left and right subtree heights found during the traversal.