The diameter is the number of edges on the longest path between any two nodes (it need not pass through the root). Implement diameter(root) in O(n) by computing each subtree's height once (post-order).
TreeNode and build are provided.
#trees#dfs2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit