Solved LeetCode program · 417 of 1,000
#0563 · Binary Tree Tilt
Original English source
Keep a shared values holding 1, 2, and 3. Keep a shared lefts holding 1, -1, and -1. Keep a shared rights holding 2, -1, and -1. Keep a shared sums holding 0, 0, and 0. Keep a shared totaltilt of 0. Keep a shared carry of 0.
To weigh from node (a whole number). If node is less than 0, set carry to 0, return. Weigh from item node of lefts. Set item node of sums to carry. Weigh from item node of rights. The gap is item node of sums minus carry. If gap is less than 0, set gap to 0 minus gap. Add gap to totaltilt. Increase item node of sums by carry plus item node of values. Set carry to item node of sums.
Weigh from 0. Show totaltilt.
Expected stdout
1