#687

Longest Univalue Path

specialist · 975 · lc medium +32 · verified · 43.6% accepted · 4,451 likes · top 25%

Description

Given the root of a binary tree, compute the length of the longest path along which every node shares the same value. The path need not pass through the root. Path length is measured as the number of edges.

Example 1:

Input: root = [5,4,5,1,1,null,5]
Output: 2
Explanation: The shown image shows that the longest path of the same value (i.e. 5).

Example 2:

Input: root = [1,4,5,4,4,null,5]
Output: 2
Explanation: The shown image shows that the longest path of the same value (i.e. 4).

Code

1
2
3
4
5
6
7
8
9