Solved LeetCode program · 736 of 1,000
#0971 · Flip Binary Tree To Match Preorder Traversal
Original English source
Keep a shared values holding 1, 2, and 3. Keep a shared lefts holding 1, -1, and -1. Keep a shared rights holding 2, -1, and -1. Keep a shared voyage holding 1, 3, and 2. Keep a shared flips as an empty list of whole numbers. Keep a shared pending as an empty list of whole numbers.
For every slot from 0 to 99.
Place -1 into pending.
Set item 0 of pending to 0. The top is 1. The index is 0. The failed is 0.
Repeat while top is more than 0 and failed is 0.
Subtract 1 from top. The node is item top of pending. The start is index. The reach is the last position of voyage. If start is more than reach, set start to 0. If item node of values is not item start of voyage, set failed to 1. If failed is 0, add 1 to index. The position is index. If position is more than reach, set position to 0. The left is item node of lefts. The right is item node of rights. The probeleft is left. If probeleft is less than 0, set probeleft to node. The flip is 0. If left is at least 0 and item probeleft of values is not item position of voyage, set flip to 1. If flip is 1, Place item node of values into flips. If flip is 1 and left is at least 0, Set item top of pending to left, add 1 to top. If flip is 1 and right is at least 0, Set item top of pending to right, add 1 to top. If flip is 0 and right is at least 0, Set item top of pending to right, add 1 to top. If flip is 0 and left is at least 0, Set item top of pending to left, add 1 to top.
If failed is 1, show -1. If failed is 0 and the last position of flips is at least 0, For each value in flips, show value. If failed is 0 and the last position of flips is less than 0, show 0.
Expected stdout
1