Every node of a binary tree is a fairy light, and its value is the light's colour. A garland is a path along the tree's edges that uses lights of one colour only; it may climb to one node and come back down into that node's other child (a single bend), and it does not have to pass through the root. Return the number of edges in the longest possible garland. An empty tree or a single node gives 0.
Examples
7
/ \
7 2
/ / \
7 2 5
Input: root = build_tree([7, 7, 2, 7, None, 2, 5])
Output: 2
Explanation: 7 -> 7 -> 7 down the left side.
6
/ \
6 6
/ \
6 6
Input: root = build_tree([6, 6, 6, 6, None, None, 6])
Output: 4
Explanation: the garland 6 -> 6 -> 6 -> 6 -> 6 bends at the root.
Goals
- Return the best single arm to the parent while recording the best two-arm path globally
- Compare a child's value with its parent's before extending a run