Solved LeetCode program · 99 of 1,000
#0099 · Recover Binary Search Tree
Original English source
Keep a shared values holding 3, 1, 4, and 2. Keep a shared lefts holding 1, -1, 3, and -1. Keep a shared rights holding 2, -1, -1, and -1. Keep a shared order as an empty list of whole numbers. Keep a shared order count as 0.
To gather from node (a whole number). If node is less than 0, return. Gather from item node of lefts. Place node into order. Add 1 to order count. Gather from item node of rights.
Gather from 0. The firstbad is -1. The secondbad is -1. The last is order count minus 1.
For every position from 1 to last.
The before is item (position minus 1) of order. The current is item position of order. If item before of values is more than item current of values and firstbad is -1, set firstbad to before. If item before of values is more than item current of values, set secondbad to current.
Swap item firstbad of values and item secondbad of values. For each node in order, show item node of values.
Expected stdout
1 2 3 4