Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.
1 / \ 2 3 / \ 4 5
- Removing the leaves [4,5,3] would result in this tree:
1 / 2
- Now removing the leaf  would result in this tree:
- Now removing the leaf  would result in the empty tree:
Implementation: Please check the main.js snippet for the solution. If you have different approach in mind or have any suggestion for this implementation feel free to share in the comment below. Thanks!