Graph Theory37 sections · 1633 units
Open in Course

CSES 1674 Subordinates - Walkthrough

Example tree trace

Tree: 1o2,31 o 2, 3; 2o4,52 o 4, 5; 3o63 o 6. DFS visits: 44 (leaf, size 11), 55 (leaf, size 11), then 22 (size 1+1+1=31 + 1 + 1 = 3). Then 66 (leaf, size 11), then 33 (size 1+1=21 + 1 = 2). Finally 11 (size 1+3+2=61 + 3 + 2 = 6).

Subordinates: node 11 has 55, node 22 has 22, node 33 has 11, nodes 4,5,64, 5, 6 have 00. The post-order ensures children are done before parents. This ordering is what makes tree DP work.