The TransCompilator

Solved LeetCode program · 441 of 1,000

#0617 · Merge Two Binary Trees

Easy Depth-First Search chunk-09.md

Original English source

Keep a shared values holding 1, 3, 2, 5, 2, 1, 3, 0, 4, 0, and 7. Keep a shared lefts holding 1, 3, -1, -1, 5, -1, -1, -1, -1, -1, and -1. Keep a shared rights holding 2, -1, -1, -1, 6, 8, 10, -1, -1, -1, and -1. Keep a shared pieces as an empty list of text.
To merge from first (a whole number) with second (a whole number). If first is less than 0 and second is less than 0, return. If first is less than 0, return. If second is less than 0, return. Increase item first of values by item second of values. The leftfirst is item first of lefts. The leftsecond is item second of lefts. If leftfirst is less than 0, set item first of lefts to leftsecond. If leftfirst is at least 0 and leftsecond is at least 0, merge from leftfirst with leftsecond. The rightfirst is item first of rights. The rightsecond is item second of rights. If rightfirst is less than 0, set item first of rights to rightsecond. If rightfirst is at least 0 and rightsecond is at least 0, merge from rightfirst with rightsecond.

To record from node (a whole number). The piece is format whole number item node of values as text. Place piece into pieces.

Merge from 0 with 4. The queue is an empty list of whole numbers. Place 0 into queue. The head is 0. The count is 1.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head. If node is less than 0, Place "null" into pieces. If node is at least 0, record from node, Place item node of lefts into queue, Place item node of rights into queue, add 2 to count.

Repeat while the last position of pieces is at least 0.
The tail is the last position of pieces. If item tail of pieces matches "null", remove the last item from pieces, otherwise stop.

The line is join pieces with ",". Show line.

Open in Playground Browse all 1,000 problems

Expected stdout

3,4,5,5,4,null,7