The TransCompilator

Solved LeetCode program · 378 of 1,000

#0508 · Most Frequent Subtree Sum

Medium Depth-First Search chunk-08.md

Original English source

Keep a shared values holding 5, 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 allsums as an empty list of whole numbers. Keep a shared occurrences of 0.

To score from node (a whole number). If node is less than 0, return. The left is item node of lefts. The right is item node of rights. If left is at least 0, score from left. If right is at least 0, score from right. The total is item node of values. If left is at least 0, set total to total plus item left of sums. If right is at least 0, set total to total plus item right of sums. Set item node of sums to total. Place total into allsums.

To tally from spot (a whole number) with target (a whole number). If spot is more than the last position of allsums, return. If item spot of allsums is target, add 1 to occurrences. Tally from spot plus 1 with target.

Score from 0. The best is 0.
For each value in allsums.
Set occurrences to 0. Tally from 0 with value. If occurrences is more than best, set best to occurrences.

The answers is an empty list of whole numbers.
For each value in allsums.
Set occurrences to 0. Tally from 0 with value. If occurrences is best and answers does not contain value, Place value into answers.

Sort answers. For each value in answers, show value.

Open in Playground Browse all 1,000 problems

Expected stdout

-3
2
4