Solved LeetCode program · 587 of 1,000
#0814 · Binary Tree Pruning
Original English source
Keep a shared values holding 1, 0, 0, and 1. Keep a shared lefts holding -1, 2, -1, and -1. Keep a shared rights holding 1, 3, -1, and -1. Keep a shared alive holding 0, 0, 0, and 0. Keep a shared kept holding 1, 1, 1, and 1.
To trim from node (a whole number). If node is less than 0, return. The left is item node of lefts. The right is item node of rights. If left is at least 0, trim from left. If right is at least 0, trim from right. The present is item node of values. If left is at least 0 and item left of alive is 1, set present to 1. If right is at least 0 and item right of alive is 1, set present to 1. Set item node of alive to present. If present is 0, set item node of kept to 0. If left is at least 0 and item left of alive is 0, set item node of lefts to -1. If right is at least 0 and item right of alive is 0, set item node of rights to -1.
Trim from 0. The queue is an empty list of whole numbers. Place 0 into queue. The head is 0. The count is 1. The pieces is an empty list of text.
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 "null" into pieces. If node is at least 0, The piece is format whole number item node of values as text, Place piece into pieces. If node is at least 0 and item node of kept is 1, Place item node of lefts into queue, Place item node of rights into queue, add 2 to count.
Repeat while the last position of pieces is at least 0.
The tail is the last position of pieces. The tailpiece is item tail of pieces. If tailpiece matches "null", remove the last item from pieces, otherwise stop.
The line is join pieces with ",". Display line.
Expected stdout
1,null,0,null,1