0
0
DSA Goprogramming~10 mins

Diameter of Binary Tree in DSA Go - 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 at this node = left height + right height
Update max diameter if current is larger
Return height = max(left height, right height) + 1
Repeat for left and right child nodes recursively
Final max diameter is the answer
We recursively find the height of left and right subtrees at each node, update the maximum diameter found, and return the height to parent nodes.
Execution Sample
DSA Go
func diameterOfBinaryTree(root *TreeNode) int {
  maxDiameter := 0
  var height func(node *TreeNode) int
  height = func(node *TreeNode) int {
    if node == nil { return 0 }
    left := height(node.Left)
    right := height(node.Right)
    if left + right > maxDiameter { maxDiameter = left + right }
    if left > right {
      return left + 1
    }
    return right + 1
  }
  height(root)
  return maxDiameter
}
This code calculates the diameter of a binary tree by recursively computing subtree heights and tracking the maximum sum of left and right heights.
Execution Table
StepOperationNode VisitedLeft HeightRight HeightDiameter at NodeMax DiameterReturn HeightVisual State
1Visit node1 (root)???0?[1]
2Visit left child2???0?[1 -> 2]
3Visit left child400001[1 -> 2 -> 4]
4Return from node400001[1 -> 2 -> 4]
5Visit right child500001[1 -> 2 -> 5]
6Return from node500001[1 -> 2 -> 5]
7Calculate diameter211222[1 -> 2 -> 4,5]
8Return from node211222[1 -> 2 -> 4,5]
9Visit right child3???2?[1 -> 3]
10Visit left child600021[1 -> 3 -> 6]
11Return from node600021[1 -> 3 -> 6]
12Visit right child700021[1 -> 3 -> 7]
13Return from node700021[1 -> 3 -> 7]
14Calculate diameter311222[1 -> 3 -> 6,7]
15Return from node311222[1 -> 3 -> 6,7]
16Calculate diameter122443[1 -> 2,3 -> 4,5,6,7]
17Return from node122443[1 -> 2,3 -> 4,5,6,7]
18End----4-[Final diameter = 4]
💡 Traversal complete, max diameter found is 4 (longest path between two nodes).
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 7After Step 11After Step 14After Step 16Final
maxDiameter00022244
left (height)?1111122
right (height)??111122
return height?1121233
Key Moments - 3 Insights
Why do we add left and right heights to get diameter at a node?
Because the diameter is the longest path through that node, which is the sum of the longest path down the left subtree and the longest path down the right subtree. See execution_table rows 7, 14, and 16 where diameter at node is left height + right height.
Why do we return max(left, right) + 1 as height?
Height is the longest path from the node down to a leaf. We take the larger height of left or right subtree and add 1 for the current node. This is shown in execution_table rows 4, 6, 8, 11, 13, 15, and 17.
How does maxDiameter get updated during recursion?
At each node, we check if left height + right height is greater than current maxDiameter and update it. This happens in rows 7, 14, and 16 where maxDiameter changes from 0 to 2 and then to 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what is the diameter at node 2?
A0
B1
C2
D3
💡 Hint
Check the 'Diameter at Node' column in row 7 of execution_table.
At which step does maxDiameter first update from 0 to 2?
AStep 7
BStep 3
CStep 14
DStep 16
💡 Hint
Look at the 'Max Diameter' column in execution_table rows to find when it changes from 0 to 2.
If the tree had no right subtree, how would maxDiameter change at the root?
AIt would be zero
BIt would be left height + right height (which is zero)
CIt would be equal to left height
DIt would be one
💡 Hint
Recall diameter at node is left height + right height; if right height is zero, diameter equals left height + 0.
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 subtrees
- Update max diameter during traversal
- Return height = max(left, right) + 1
- Final max diameter is answer
Full Transcript
The diameter of a binary tree is the longest path between any two nodes. We find it by recursively calculating the height of left and right subtrees at each node. The diameter at a node is the sum of left and right subtree heights. We keep track of the maximum diameter found during traversal. The height returned to parent nodes is the maximum of left and right heights plus one for the current node. This process continues until all nodes are visited, and the maximum diameter found is returned as the result.