Solved LeetCode program · 838 of 1,000
#1110 · Delete Nodes And Return Forest
Original English source
Keep a shared values holding 1, 2, 3, 4, 5, 6, and 7. Keep a shared lefts holding 1, 3, 5, -1, -1, -1, and -1. Keep a shared rights holding 2, 4, 6, -1, -1, -1, and -1. Keep a shared removed holding 0, 0, 0, 0, 0, 0, and 0. Set item 2 of removed to 1. Set item 4 of removed to 1. Keep a shared roots as an empty list of whole numbers. Keep a shared order as an empty list of whole numbers. Keep a shared begin of 0. Keep a shared final of 0.
To discard from node (a whole number). The left is item node of lefts. The right is item node of rights. If left is at least 0, Prune from left with 1. If right is at least 0, Prune from right with 1.
To prune from node (a whole number) with rootflag (a whole number). If node is less than 0, return. If item node of removed is 1, Discard from node, return. If rootflag is 1, Place node into roots. The left is item node of lefts. The right is item node of rights. If left is at least 0 and item left of removed is 0, Prune from left with 0. If left is at least 0 and item left of removed is 1, Set item node of lefts to -1, Prune from left with 1. If right is at least 0 and item right of removed is 0, Prune from right with 0. If right is at least 0 and item right of removed is 1, Set item node of rights to -1, Prune from right with 1.
To walk from start (a whole number). The queue is a list of 64 zeros. Set item 0 of queue to start. 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. The content is 0. The left is 0. The right is 0. If node is at least 0, set content to item node of values. If node is at least 0, set left to item node of lefts. If node is at least 0, set right to item node of rights. If node is less than 0, Place -1 into order. If node is at least 0, Place content into order. If node is at least 0, The slotone is count, Set item slotone of queue to left, Add 1 to count. If node is at least 0, The slottwo is count, Set item slottwo of queue to right, Add 1 to count.
To trim. The spot is the last position of order. Set final to spot.
Repeat while spot is at least begin and item spot of order is -1.
Decrease spot by 1. Set final to spot.
To display. The parts is an empty list of text.
For every slot from begin to final.
The value is item slot of order. The piece is format whole number value as text. Place piece into parts. If slot is final, The line is join parts with ",", Show line.
Prune from 0 with 1.
For every which from 0 to the last position of roots.
The start is item which of roots. The prior is the last position of order. Set begin to prior plus 1. Walk from start. Trim. Display.
Expected stdout
1,2,-1,4 6 7