Dashboard
LC #543

Diameter of Binary Tree

Pattern · ?AmazonGoogleMetaBloomberg

Problem

Find the length of the longest path between any two nodes in the tree (the diameter).

Constraints

  • 1 ≤ n ≤ 10⁴
  • -100 ≤ Node.val ≤ 100
  • Path does not need to pass through root

Example

Input
root = [1, 2, 3, 4, 5]
Output
3
Why
Longest path is [4,2,1,3] or [5,2,1,3], length = 3 edges

Spot the pattern

Which pattern cracks it?

Hints

One at a time
Solve on LeetCode ↗