Solved LeetCode program · 762 of 1,000
#0998 · Maximum Binary Tree II
Original English source
Keep a shared values holding 4, 1, 3, 2, and 0. Keep a shared lefts holding 1, -1, 3, -1, and -1. Keep a shared rights holding 2, -1, -1, -1, and -1. Keep a shared desired of 5. Keep a shared spot of 0.
To descend from current (a whole number). Set spot to current. The right is item current of rights. If right is at least 0 and item right of values is more than desired, Descend from right.
The root is 0.
If desired is more than item 0 of values.
Set item 4 of values to desired. Set item 4 of lefts to root. Set item 4 of rights to -1. Set root to 4.
If desired is at most item 0 of values.
Descend from 0. The branch is spot. The hole is item branch of rights. Set item 4 of values to desired. Set item 4 of lefts to hole. Set item 4 of rights to -1. Set item branch of rights to 4.
The queue is a list of 16 zeros. Set item 0 of queue to root. The head is 0, while the count is 1. The order is an empty list of whole numbers.
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 -1 into order.
If node is at least 0.
Place item node of values into order. Set item count of queue to item node of lefts. Add 1 to count. Set item count of queue to item node of rights. Add 1 to count.
The cut is the last position of order.
Repeat while cut is at least 0 and item cut of order is -1.
Decrease cut by 1.
For every spot from 0 to cut.
Show item spot of order.
Expected stdout
5 4 -1 1 3 -1 -1 2