The TransCompilator

Solved LeetCode program · 860 of 1,000

#1161 · Maximum Level Sum of a Binary Tree

Medium Depth-First Search chunk-18.md

Original English source

The values is a list of 1, 7, 0, 7, -8, -1000000, and -1000000. The count is 7. The empty is -1000000. The lefts is an empty list of whole numbers. The rights is an empty list of whole numbers.
For every node from 0 to 6.
The left is node times 2 plus 1. The right is node times 2 plus 2. If left is at least count, set left to -1. If right is at least count, set right to -1. Place left into lefts. Place right into rights.

The queue is an empty list of whole numbers. The depths is an empty list of whole numbers. Place 0 into queue. Place 1 into depths. The head is 0. The held is 1. The level is 1. The leveltotal is 0. The best is -1000000. The answer is 1.
Repeat while head is less than held.
The front is head. The node is item front of queue. The depth is item front of depths. If depth is not level, Set best to the larger of best and leveltotal, Set answer to level, Set level to depth, Set leveltotal to 0. Increase leveltotal by item node of values. The left is item node of lefts. The right is item node of rights. If left is at least 0 and item left of values is not -1000000, Place left into queue, Place (depth plus 1) into depths, add 1 to held. If right is at least 0 and item right of values is not -1000000, Place right into queue, Place (depth plus 1) into depths, add 1 to held. Add 1 to head.

If leveltotal is more than best, Set best to leveltotal, Set answer to level. Show answer.

Open in Playground Browse all 1,000 problems

Expected stdout

2