The TransCompilator

Solved LeetCode program · 607 of 1,000

#0834 · Sum of Distances in Tree

Hard Dynamic Programming chunk-13.md

Original English source

Keep a shared lefts holding 0, 0, 2, 2, and 2. Keep a shared rights holding 1, 2, 3, 4, and 5. Keep a shared sizes as an empty list of whole numbers. Keep a shared answers as an empty list of whole numbers. Keep a shared travel of 0. Keep a shared gain of 0.
For every place from 0 to 5.
Place 0 into sizes. Place 0 into answers.

To measure from node (a whole number) with parent (a whole number). Set item node of sizes to 1.
For every edge from 0 to 4.
The first is item edge of lefts. The second is item edge of rights. If first is node and second is not parent, Measure from second with node. If first is node and second is not parent, Set item node of sizes to item node of sizes plus item second of sizes. If second is node and first is not parent, Measure from first with node. If second is node and first is not parent, Set item node of sizes to item node of sizes plus item first of sizes.

To gather from node (a whole number) with parent (a whole number) and depth (a whole number). Increase travel by depth.
For every edge from 0 to 4.
The first is item edge of lefts. The second is item edge of rights. If first is node and second is not parent, Gather from second with node and depth plus 1. If second is node and first is not parent, Gather from first with node and depth plus 1.

To spread from node (a whole number) with parent (a whole number).
For every edge from 0 to 4.
The first is item edge of lefts. The second is item edge of rights. If first is node and second is not parent, Set gain to 6 minus 2 times item second of sizes. If first is node and second is not parent, Set item second of answers to item node of answers plus gain. If first is node and second is not parent, Spread from second with node. If second is node and first is not parent, Set gain to 6 minus 2 times item first of sizes. If second is node and first is not parent, Set item first of answers to item node of answers plus gain. If second is node and first is not parent, Spread from first with node.

Measure from 0 with -1. Set travel to 0. Gather from 0 with -1 and 0. Set item 0 of answers to travel. Spread from 0 with -1. The line is empty text.
For every node from 0 to 5.
If node is more than 0, Place "," into line. The piece is format whole number item node of answers as text. Place piece into line.

Show line.

Open in Playground Browse all 1,000 problems

Expected stdout

8,12,6,10,10,10