FreeCareerPath
Build the PrimitiveEasy

Diameter of Binary Tree

Write `diameter_of_binary_tree(root)` returning the number of edges on the longest path between any two nodes. The path need not pass through the root. [1,2,3,4,5] -> 3 (4 -> 2 -> 1 -> 3) The trick is that the recursion returns one thing and records another. It returns the depth of the subtree to its parent, but along the way it updates a running best with `left + right` — the path that turns around at this node. Confusing those two is why this problem is asked.

What to expect: A timer starts when you begin. Edit the starter code, run it against the test suite as many times as you like, then finish when you're done. The reference solution and interviewer follow-up questions unlock only after you finish.

Log in to start this session

Timed sessions and your results for "Diameter of Binary Tree" are saved to your account — logging in takes a few seconds.

Diameter of Binary Tree — FreeCareerPath