Skip to content
AI360Xpert
Beta
Difficulty: EasyTrees

Diameter of Binary Tree

Problem in Plain English

Given the root of a binary tree, return the number of edges on the longest path between any two nodes. That longest path may or may not pass through the root.

Problem Statement

Given the root of a binary tree, return the length of the diameter of the tree.

The diameter is the longest path between any two nodes, measured in edges rather than nodes, and it may or may not pass through the root.

Constraints

  • The number of nodes is between 1 and 10000, inclusive.
  • Each node value is between -100 and 100, inclusive; only the tree shape affects the diameter.

Examples

Example 1

Input
root = [1,2,3,4,5]
Output
3

The path 4 → 2 → 1 → 3 has 3 edges. Path 5 → 2 → 1 → 3 ties it. The two child heights at root 1 are 2 and 1, while the candidate at node 2 has only 2 edges.

Example 2

Input
root = [1,2]
Output
1

The only nonempty path joins 1 to 2 with one edge. Root 1 combines child heights 1 and 0; node 2 contributes a candidate of 0.

Example 3

Input
root = [1]
Output
0

A single node has no edge to traverse. Both child heights are 0, so the diameter is 0 even though the tree height is 1.

Example 4

Input
root = [1,2,null,3,4,5,null,null,6]
Output
4

The longest path is 5 → 3 → 2 → 4 → 6, entirely below root 1. At node 2 the child heights are 2 and 2, giving 4 edges; root 1 combines heights 3 and 0, giving only 3.

Intuition

A path between two nodes has one highest node, where its two downward arms meet. Height counts nodes below a child, with an empty child having height 0: those child heights also count the edges from their parent to the deepest descendants. In Example 1, root 1 joins left height 2 and right height 1 into 3 edges. The winning highest node can be below the root, so compare this sum at every node; return only a single downward height to its parent.

Approaches

Top Down Recompute

Solution Details

Reveal Top Down Recompute: intuition, complexity, and code

Hints

Hint 1
The longest path through one node joins its deepest left descent with its deepest right descent.
Hint 2
Check that candidate at every node, because the winning path can hide entirely inside one subtree.
Hint 3
Compute heights bottom-up so each subtree height is measured once and reused by its parent.

Edge Cases

  • A single node has height 1 but diameter 0, because the answer counts edges.
  • A 10000-node left, right or alternating chain has diameter 9999; explicit stacks avoid deep recursive calls.
  • The longest path can be wholly inside one subtree, as in Example 4; checking only the root misses it.
  • Duplicate, negative and zero values change neither heights nor diameter; node identity, not value, defines the structure.
  • Although the stated input is nonempty, these snippets also return 0 for null. Every input node and child reference stays unchanged across repeated calls.

Common Mistakes and Interview Tips

  • Adding an extra 1 to the child-height sum: with empty height 0, leftHeight + rightHeight already counts edges.
  • Returning the diameter candidate as the height sent upward; a parent can extend only one child arm, so return 1 + max(leftHeight, rightHeight).
  • Resetting best at each frame, or retaining it as an uncleared object field between calls; keep it local to the current invocation.
  • Combining a frame before both child heights are available, or failing to set last to 0 for an absent child, reuses an unrelated previous height.
  • Claiming the recomputation approach is linear or that explicit stacks use constant space: repeated descents cost quadratic time on a chain, and a deep post-order walk stores O(h) frames.
  • Assuming recursive snippets fit the valid 10000-node depth. Explain post-order first, then use explicit frames when runtime call-stack limits are tighter than the input bound.

Key Takeaway

Return child summaries upward while updating a shared best, which turns repeated top-down measuring into one bottom-up pass.