Skip to main content
Path Problems

Diameter of Tree

The diameter is the longest path between any two nodes (measured in edges). At each node, it's leftHeight + rightHeight. Track the global max. Return 1 + max(leftHeight, rightHeight) to the parent.

O(n)
·
O(n)

How It Works

The diameter is the longest path between any two nodes, counted in edges, and the longest path in the whole tree must pass through some node as its highest point. At that node, the path length equals the left subtree's height plus the right subtree's height. So a single post-order traversal computes each node's height, and at every node it checks leftHeight + rightHeight against a global maximum. The function returns 1 + max(leftHeight, rightHeight) so the parent can continue the same computation.

This piggybacks the diameter check onto the height calculation, visiting each node once for O(n) time and O(h) stack space. The naive alternative — computing height separately at every node — repeats work on shared subtrees and degrades to O(n^2) on skewed trees. The same skeleton generalizes to longest univalue path and similar problems where the answer arches over a node.

Step-by-Step Visualization

Tree as level-order: [1, 2, 3, 4, 5]
1
0
2
1
3
2
4
3
5
4
Root1
Diameter0
1/5

Code

Java
static int diameter;

static int diameterOfBinaryTree(TreeNode root) {
  diameter = 0;
  height(root);
  return diameter;
}

static int height(TreeNode node) {
  if (node == null) return 0;
  int left = height(node.left);
  int right = height(node.right);
  diameter = Math.max(diameter, left + right);
  return 1 + Math.max(left, right);
}

// Tree:     1
//          / //         2   3   → diameter = 3 (path: 4→2→1→3)
//        / //       4   5

Tips & Gotchas

1Diameter = longest path between any two nodes in a tree
2At each node: diameter through it = leftHeight + rightHeight
3Track global max diameter while computing heights recursively

Practice Problems

  • 1Diameter of Binary Tree
  • 2Longest Univalue Path
  • 3Binary Tree Maximum Path Sum
  • 4Diameter of N-Ary Tree

About the Path Problems Pattern

Find paths in a tree that optimize some value (max sum, min length, specific target). Paths can go root-to-leaf, or through any nodes. The key insight: at each node, the best path either goes through it or doesn't.

Key insight

Tree problems are almost always DFS (recursion) or BFS (level-order). The pattern: solve for children, combine results, return up. BST's sorted property lets you prune half the tree.

Common Trees Interview Problems

  • Maximum Depth of Binary Tree
  • Validate BST
  • Binary Tree Level Order Traversal
  • Lowest Common Ancestor
  • Serialize and Deserialize Binary Tree
  • Diameter of Binary Tree

Frequently Asked Questions

Why isn't the diameter just the height-based path through the root?

The longest path may live entirely inside one deep subtree and never touch the root. That is why every node checks its own leftHeight + rightHeight against the running maximum — the diameter's peak node can be anywhere in the tree.

Should I count nodes or edges when reporting a diameter?

Conventions differ by problem, so read the statement carefully. LeetCode's Diameter of Binary Tree counts edges, which is leftHeight + rightHeight with heights measured in edges; a node count is simply that value plus one. Off-by-one errors here are the most common bug.