Solved LeetCode program · 792 of 1,000
#1028 · Recover a Tree From Preorder Traversal
Original English source
Keep a shared traversal as empty text. Place "1-2--3--4-5--6--7" into traversal. Keep a shared values as an empty list of whole numbers. Keep a shared depths as an empty list of whole numbers. Keep a shared lefts as an empty list of whole numbers. Keep a shared rights as an empty list of whole numbers. Keep a shared stack as an empty list of whole numbers. The number is 0. The depth is 0. The inside is 0. The gaps is 0.
For every spot from 0 to the last position of traversal.
The symbol is the slice of traversal from spot to spot plus 1. The dash is 0. If symbol is "-", set dash to 1. If dash is 1 and inside is 1, Place number into values, Place depth into depths, set number to 0, set inside to 0, set depth to 0. If dash is 1, add 1 to depth. If dash is 0, The digit is the digit value of character spot of traversal, set number to number times 10 plus digit, set inside to 1.
If inside is 1, Place number into values, Place depth into depths.
For every spot from 0 to the last position of values.
Place -1 into lefts. Place -1 into rights.
To trim with level (a whole number). The settled is 0.
Repeat while settled is 0.
The end is the last position of stack. If end is less than 0, set settled to 1, stop. If item (item end of stack) of depths is less than level, set settled to 1, stop. Remove the last item from stack.
Place 0 into stack.
For every node from 1 to the last position of values.
Trim with item node of depths. The end is the last position of stack. The parent is item end of stack. If item parent of lefts is less than 0, set item parent of lefts to node, otherwise set item parent of rights to node. Place node into stack.
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. Show item node of values. The left is item node of lefts. The right is item node of rights. If left is at least 0, Place left into queue, add 1 to count. If right is at least 0, Place right into queue, add 1 to count.
Expected stdout
1 2 5 3 4 6 7