The diameter of a binary tree is the length of the longest path between any
two nodes, counted in edges. The path may or may not pass through the root.
solve(levelOrder) receives the tree serialized level by level and returns its
diameter.
Examples
solve([1, 2, 3, 4, 5]) → 3 // 4 → 2 → 1 → 3 (or 5 → 2 → 1 → 3)
solve([1, 2]) → 1
solve([1]) → 0
solve([1, 2, null, 3, 4, 5, null, null, 6]) → 4 // 5 → 3 → 2 → 4 → 6, below the rootCalling height() at every node is O(n²). Aim for a single pass: compute
heights bottom-up and update the best diameter as you go.
Sample tests