Data structures · Trees and traversals
One pattern answers most questions about a tree: answer for the empty tree, then build each node's answer from its two subtrees.