Solved LeetCode program · 443 of 1,000
#0623 · Add One Row to Tree
Original English source
Keep a shared values holding 4, 2, 6, 3, 1, and 5. Keep a shared lefts holding 1, 3, 5, -1, -1, and -1. Keep a shared rights holding 2, 4, -1, -1, -1, and -1. Keep a shared inserted of 1. Keep a shared depth of 2. Keep a shared made of 0. Keep a shared root of 0. Keep a shared pieces as an empty list of text.
To sprout. Place inserted into values. Place -1 into lefts. Place -1 into rights. Set made to the last position of values.
To fill from node (a whole number). The oldleft is item node of lefts. Sprout. Set item made of lefts to oldleft. Set item node of lefts to made. The oldright is item node of rights. Sprout. Set item made of rights to oldright. Set item node of rights to made.
To graft from node (a whole number) with level (a whole number). If node is less than 0, return. If level is depth minus 1, fill from node, return. If item node of lefts is at least 0, graft from item node of lefts with level plus 1. If item node of rights is at least 0, graft from item node of rights with level plus 1.
To record from node (a whole number). The piece is format whole number item node of values as text. Place piece into pieces.
If depth is 1, The oldroot is root, Sprout, Set item made of lefts to oldroot, Set root to made. If depth is more than 1, Graft from root with 1. The queue is an empty list of whole numbers. Place root 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.
Expected stdout
4,1,1,2,null,null,6,3,1,5