Loading problem...
You are given the root of a binary tree. Your task is to determine the length of the longest path in the tree where every node along the path contains the same value. This path may traverse through the root node, but it is not required to do so.
Important: The length of a path is defined as the number of edges between the starting and ending nodes of the path.
The path can extend in any direction—it can go from a descendant through an ancestor to another descendant, forming an inverted "V" shape. However, each node can only appear once in any valid path.
root = [5,4,5,1,1,null,5]2The longest uniform path consists of nodes with value 5. Starting from the root (5), we can go right to another 5, giving us a path of length 2 (two edges: root → right child → right child's right child). Note that the left subtree contains values 4, 1, and 1, which don't form a longer uniform path.
root = [1,4,5,4,4,null,5]2The longest uniform path is formed by nodes with value 4 in the left subtree. Starting from node 4 (left child of root), both of its children are also 4. This creates a path of length 2 going left-child → parent (4) → right-child, with 2 edges total.
root = [1,1,1]2All three nodes have value 1. The longest path goes from the left child, through the root, to the right child. This path contains 2 edges (left child → root → right child), giving us a path length of 2.
Constraints