0
0
DSA Cprogramming~10 mins

DP on Trees Diameter of Tree in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - DP on Trees Diameter of Tree
Start at any node
DFS to children
Compute max depths from children
Update diameter using top two depths
Return max depth to parent
Repeat for all nodes
Final diameter is max found
We start DFS from any node, compute max depths from children, update diameter using top two depths, and return max depth to parent until all nodes are processed.
Execution Sample
DSA C
int dfs(int node, int parent) {
  int max1 = 0, max2 = 0;
  for (each child of node) {
    if (child != parent) {
      int depth = dfs(child, node);
      if (depth > max1) { max2 = max1; max1 = depth; } else if (depth > max2) max2 = depth;
    }
  }
  diameter = max(diameter, max1 + max2);
  return max1 + 1;
}
This code runs DFS to find max depths from children, updates diameter with sum of top two depths, and returns max depth to parent.
Execution Table
StepOperationNode VisitedMax Depths from ChildrenDiameter UpdateReturn DepthVisual State
1Start DFSNode 1max1=0, max2=0diameter=0Returns after children1
2DFS child Node 2Node 2max1=0, max2=0diameter=0Returns 11 -> 2
3Update Node 1 depthsNode 1max1=1, max2=0diameter=1Returns 21 -> 2
4DFS child Node 3Node 3max1=0, max2=0diameter=1Returns 11 -> 2, 1 -> 3
5Update Node 1 depthsNode 1max1=1, max2=1diameter=2Returns 21 -> 2, 1 -> 3
6DFS child Node 4Node 4max1=0, max2=0diameter=2Returns 11 -> 2, 1 -> 3, 3 -> 4
7Update Node 3 depthsNode 3max1=1, max2=0diameter=2Returns 21 -> 2, 1 -> 3, 3 -> 4
8Update Node 1 depthsNode 1max1=2, max2=1diameter=3Returns 31 -> 2, 1 -> 3, 3 -> 4
9DFS child Node 5Node 5max1=0, max2=0diameter=3Returns 13 -> 5
10Update Node 3 depthsNode 3max1=1, max2=1diameter=3Returns 23 -> 4, 3 -> 5
11Update Node 1 depthsNode 1max1=2, max2=2diameter=4Returns 31 -> 2, 1 -> 3, 3 -> 4, 3 -> 5
12End DFSNode 1max1=2, max2=2diameter=4Final diameter=4Full tree
💡 All nodes visited, diameter updated with max sum of two depths, DFS ends.
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 8After Step 11Final
diameter002344
max1 (Node 1)011222
max2 (Node 1)001122
Return Depth (Node 1)N/A22333
Key Moments - 3 Insights
Why do we keep track of two maximum depths (max1 and max2) at each node?
Because the diameter can pass through the current node combining two longest paths from different children. See execution_table steps 5 and 11 where diameter updates use max1 + max2.
Why do we return max1 + 1 from the DFS function?
We return max1 + 1 to represent the longest path from current node down to a leaf including the current node itself. This is shown in execution_table steps 3, 5, 8, and 11.
Why do we ignore the parent node in DFS traversal?
To avoid revisiting the node we came from and prevent infinite loops. This is implied in the code and shown in execution_table where children are visited excluding parent.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is the diameter value after updating Node 1?
A2
B1
C3
D0
💡 Hint
Check the 'Diameter Update' column at step 5 in execution_table.
At which step does the diameter first become 4?
AStep 8
BStep 6
CStep 11
DStep 12
💡 Hint
Look at the 'Diameter Update' column in execution_table rows for diameter=4.
If we did not track max2 (second max depth), what would be the diameter after step 11?
A4
B3
C2
D1
💡 Hint
Without max2, diameter updates use only max1, see how diameter changes in execution_table.
Concept Snapshot
DP on Trees Diameter:
- Use DFS from any node
- Track top two max depths from children
- Update diameter = max(diameter, max1 + max2)
- Return max1 + 1 to parent
- Diameter is longest path between any two nodes
Full Transcript
We find the diameter of a tree using dynamic programming with DFS. Starting from any node, we explore children and compute the maximum depths from them. At each node, we keep track of the two largest depths (max1 and max2). The diameter is updated by the sum of these two depths, representing the longest path passing through that node. We return max1 + 1 to the parent to represent the longest path including the current node. This process repeats until all nodes are visited, and the maximum diameter found is the tree's diameter.