The TransCompilator

Solved LeetCode program · 744 of 1,000

#0979 · Distribute Coins in Binary Tree

Medium Depth-First Search chunk-15.md

Original English source

Keep a shared values holding 0, 3, and 0. Keep a shared lefts holding 1, -1, and -1. Keep a shared rights holding 2, -1, and -1. Keep a shared moves of 0. Keep a shared flow of 0.

To settle from node (a whole number). If node is less than 0, set flow to 0, return. Settle from item node of lefts. The leftshare is flow. Settle from item node of rights. The rightshare is flow. The surplus is item node of values plus leftshare plus rightshare minus 1. Set flow to surplus. The gap is the absolute value of surplus. Increase moves by gap.

Settle from 0. Show moves.

Open in Playground Browse all 1,000 problems

Expected stdout

3