#543

Diameter of Binary Tree

pupil · 335 · lc easy +22 · verified · 65.1% accepted · 15,568 likes · top 69%

play →

Description

Given the root of a binary tree, return the tree's diameter — the number of edges along the longest path connecting any two nodes. This path does not need to go through the root.

Example 1:

Input: root = [1,2,3,4,5]
Output: 3
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].

Example 2:

Input: root = [1,2]
Output: 1

Code

1
2
3
4
5
6
7
8
9