Solved LeetCode program · 103 of 1,000
#0103 · Binary Tree Zigzag Level Order Traversal
Original English source
Keep a shared values holding 3, 9, 20, 15, and 7. Keep a shared lefts holding 1, -1, 3, -1, and -1. Keep a shared rights holding 2, -1, 4, -1, and -1. Keep a shared rows as an empty list of text. Keep a shared rung as an empty list of text. Keep a shared line as empty text. Keep a shared depth of 0. Keep a shared backward of 0. Keep a shared rungcount of 0.
To stitch from spot (a whole number) with last (a whole number). If spot is more than last, return. The source is spot. If backward is 1, set source to last minus spot. If the length of line is more than 0, Place "," into line. Place item source of rung into line. Stitch from spot plus 1 with last.
To finish level. Set line to empty text. Set backward to 0. If depth modulo 2 is 1, set backward to 1. Stitch from 0 with rungcount minus 1. Place line into rows. Add 1 to depth.
For every filler from 0 to 19, Place "" into rung.
The queue is an empty list of whole numbers. Place 0 into queue. The head is 0. The count is 1. The remaining is 1. The nextcount is 0.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head. The piece is format whole number item node of values as text. Set item rungcount of rung to piece. Add 1 to rungcount. If item node of lefts is at least 0, Place item node of lefts into queue, add 1 to count, add 1 to nextcount. If item node of rights is at least 0, Place item node of rights into queue, add 1 to count, add 1 to nextcount. Subtract 1 from remaining. If remaining is 0, finish level, set rungcount to 0, set remaining to nextcount, set nextcount to 0.
For each item in rows, show item.
Expected stdout
3 20,9 15,7