← All problems

Diameter of Binary Tree

Easy treesrecursion LeetCode ↗
Given a binary tree, find the length (in edges) of the longest path between any two nodes. The path need not pass through the root. This is the tree's diameter.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(n), Space O(h)

Common pitfalls

  • Returning the path instead of its length
  • Counting nodes instead of edges

Your solution

Log in to save your code and track progress.