LeetCode #543 Easy

Diameter of Binary Tree

Return the length (in edges) of the longest path between any two nodes — it need not pass the root.

treedfsrecursion
Open on LeetCode ↗
02

Intuition

The longest path bends at some node: left depth + right depth through it. Compute depth recursively and, at every node, test leftDepth + rightDepth as a diameter candidate — one DFS answers both questions at once.

How to spot this pattern

Structurally identical to binary-tree-maximum-path-sum: what you return (depth of one arm) differs from what you record (the bend through this node). Any time the global answer can pass through a node using both children, but a parent can only receive one arm, split the two quantities like this.

03

Approach

1

Depth is the helper

depth(node) = 1 + max(depth(left), depth(right)). That's the standard recursion.

2

Diameter rides along

While unwinding, each node knows both child depths — their sum is the best path bending there. Track the global max in a side variable.

3

Return depth, record diameter

The recursion's return value stays pure (depth); the answer is collected as a side effect — a common DFS pattern.

04

Solution & live demo

1class Solution:
2 def diameterOfBinaryTree(self, root):
3 self.best = 0
4 def depth(node):
5 if not node: return 0
6 l, r = depth(node.left), depth(node.right)
7 self.best = max(self.best, l + r)
8 return 1 + max(l, r)
9 depth(root)
10 return self.best
05

Common pitfalls

Returning the combined width to the parent

✗ Wrong
return l + r
✓ Right
self.best = max(self.best, l + r)
return 1 + max(l, r)

A path that already bent through this node can't continue upward without revisiting it. The parent needs a single descending arm — the bend is recorded globally and never propagated.

Forgetting the 1 + on the returned depth

✗ Wrong
return max(l, r)
✓ Right
return 1 + max(l, r)

The current node adds one edge to whichever arm the parent extends. Without it every depth is zero and the diameter comes back 0.

Counting nodes instead of edges

✗ Wrong
self.best = max(self.best, l + r + 1)
✓ Right
self.best = max(self.best, l + r)

The problem defines diameter as the number of edges on the longest path. Since l and r are already edge counts to the deepest leaves, their sum is the answer — adding one reports the node count instead.

06

Edge cases

Single node

No edges → diameter 0.

Degenerate chain

Best 'bend' is at the top: one side deep, other 0 — equals the chain length.

07

Complexity

Time
O(n)
Space
O(h)
One post-order pass; h = tree height.