We’re preparing your current view and syncing the latest data.
Given the root of a binary tree, return the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. The length of the path between two nodes is represented by the number of edges between them.
The input is the root of a binary tree where each node contains an integer value.
Return an integer representing the length of the longest univalue path (number of edges) in the tree.
The number of nodes in the tree is in the range [0, 10^4]. -1000 <= Node.val <= 1000
Example 1
Input
[5,4,5,1,1,null,5]
Output
2
Explanation
The longest path with the same value is the path with three nodes with value 5.
Example 2
Input
[1,4,5,4,4,null,5]
Output
2
Explanation
The longest path with the same value is the path with three nodes with value 4.