Back to problems

Longest Univalue Path

Algorithm · Snowflake · Medium

Given the root node of a binary tree, determine the greatest path length for which every included node stores an identical value. Such a path does not have to include the tree's root. Measure a path by counting the edges connecting its two endpoints. Examples Example 1: Input: root = [7,3,7,2,2,null,7] Output: 2 Explanation: The chain formed by the three connected nodes with value 7 contains two edges. Example 2: Input: root = [8,6,9,6,6,null,9] Output: 2 Explanation: The…

Checking your access…