A gardener prunes a tree in rounds: each round cuts off every current leaf at once, and the next round starts on what remains, until nothing is left. For a binary tree whose nodes look like { value, left, right } (left and right are child nodes or null), record which values fall off in each round.
Write leafRounds(root), which returns an array of rounds. Round 0 holds the values of the original leaves, round 1 the values of the nodes that are leaves once round 0 is gone, and so on; the last round holds only the root's value.
Rules:
Within a round, list the values from left to right as they appear in the tree.
An empty tree (null) returns [].
Do not modify the tree.
Throw a TypeError if root, or any left or right, is neither null nor an object.
Trees can have 100,000 nodes, including a single chain 100,000 nodes deep. Removing leaves and walking the tree again for every round takes O(n²) on a chain, and a recursive walk overflows the call stack.