The TransCompilator

Solved LeetCode program · 995 of 1,000

#1373 · Maximum Sum BST in Binary Tree

Hard Dynamic Programming chunk-20.md

Original English source

Keep a shared values holding 1, 4, 3, 2, 4, 2, 5, 4, and 6. Keep a shared lefts holding 1, 3, 5, -1, -1, -1, 7, -1, and -1. Keep a shared rights holding 2, 4, 6, -1, -1, -1, 8, -1, and -1. Keep a shared valid as an empty list of whole numbers.
For every node from 0 to 8.
Place 0 into valid.

Keep a shared lows as an empty list of whole numbers.
For every node from 0 to 8.
Place 0 into lows.

Keep a shared highs as an empty list of whole numbers.
For every node from 0 to 8.
Place 0 into highs.

Keep a shared sums as an empty list of whole numbers.
For every node from 0 to 8.
Place 0 into sums.

Keep a shared value of 0. Keep a shared good of 0. Keep a shared low of 0. Keep a shared high of 0. Keep a shared total of 0. Keep a shared best of 0.

To inspectleft from child (a whole number). If child is less than 0, return. If item child of valid is 0, set good to 0, return. If item child of highs is at least value, set good to 0, return. Set low to item child of lows. Increase total by item child of sums.

To inspectright from child (a whole number). If child is less than 0, return. If item child of valid is 0, set good to 0, return. If item child of lows is at most value, set good to 0, return. Set high to item child of highs. Increase total by item child of sums.

To score from node (a whole number). If node is less than 0, return. Score from item node of lefts. Score from item node of rights. Set value to item node of values. Set good to 1. Set low to value. Set high to value. Set total to value. Inspectleft from item node of lefts. Inspectright from item node of rights. Set item node of valid to good. Set item node of lows to low. Set item node of highs to high. Set item node of sums to total. If good is 1 and total is more than best, set best to total.

Score from 0. Show best.

Open in Playground Browse all 1,000 problems

Expected stdout

20