915. Longest Univalue Path

MediumTreesBinary TreeTree DPDFS

Given the root of a binary tree, return the length (number of edges) of the longest path where every node on the path has the same value. The path need not pass through the root. The tree is given as a level-order array with null for missing children.

Input: A level-order array of the tree.

Output: Integer — the longest univalue path length in edges.

Examples

Example 1
Input: [5,4,5,1,1,null,5]
Output: 2
Explanation: Path of 5's spans 2 edges.
Example 2
Input: [1,4,5,4,4,null,5]
Output: 2
Explanation: Path of 4's spans 2 edges.
Example 3
Input: [1]
Output: 0
Explanation: Single node, no edges.

Constraints

Asked by

GoogleAmazon
Solve this problem in the editor →