Solved LeetCode program · 772 of 1,000
#1008 · Construct Binary Search Tree from Preorder Traversal
Original English source
Keep a shared preorder holding 8, 5, 1, 7, 10, and 12. Keep a shared lefts holding -1, -1, -1, -1, -1, and -1. Keep a shared rights holding -1, -1, -1, -1, -1, and -1. Keep a shared next slot as 0. Keep a shared child of 0.
To build from bound (a whole number). Set child to -1. If next slot is more than the last position of preorder, return. The slot is next slot. The current is item slot of preorder. If current is more than bound, return. Add 1 to next slot. Build from current. Set item slot of lefts to child. Build from bound. Set item slot of rights to child. Set child to slot.
Build from 1000000. The queue is an empty list of whole numbers. The order 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. Place node into order. If node is at least 0, Place item node of lefts into queue, Place item node of rights into queue, add 2 to count.
The pieces is an empty list of text.
For each node in order.
If node is less than 0, Place "null" into pieces, otherwise The piece is format whole number item node of preorder as text, Place piece into pieces.
The tail is the last position of pieces.
Repeat while tail is at least 0.
If item tail of pieces matches "null", remove the last item from pieces, set tail to tail minus 1, otherwise stop.
The line is join pieces with ",". Show line.
Expected stdout
8,5,10,1,7,null,12