LC: 366. Find Leaves of Binary Tree - spiralgo/algorithms GitHub Wiki

366. Find Leaves of Binary Tree:

The Essence:

The searched nodes are grouped according to their height, i.e. their distance from the leaves of the initial tree. This is basically 1 + the maximum of the height of its left and right nodes.

Details:

The solution can be implemented recursively. To keep the order, first the height of the left child then the right child of a node is sought. The node's height is then determined. A new list is then created in the return value if it does not contain one for this level. The node's value is then put into the new or the present list.