This website contains ALL LeetCode **Premium** problems for
**FREE!!**.

All leaked interview problems are collected from Internet.

All leaked interview problems are collected from Internet.

Given a binary tree, find 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.

**Note:** The length of path between two nodes is represented by the number of edges between them.

**Example 1:**

Input:

5 / \ 4 5 / \ \ 1 1 5

Output:

2

**Example 2:**

Input:

1 / \ 4 5 / \ \ 4 4 5

Output:

2

**Note:**
The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.

b'

\n\n#### Approach #1: Recursion [Accepted]

\n

\n

'
**Intuition**

We can think of any path (of nodes with the same values) as up to two arrows extending from it\'s root.

\nSpecifically, the *root* of a path will be the unique node such that the parent of that node does not appear in the path, and an *arrow* will be a path where the root only has one child node in the path.

Then, for each node, we want to know what is the longest possible arrow extending left, and the longest possible arrow extending right? We can solve this using recursion.

\n**Algorithm**

Let `arrow_length(node)`

be the length of the longest arrow that extends from the `node`

. That will be `1 + arrow_length(node.left)`

if `node.left`

exists and has the same value as `node`

. Similarly for the `node.right`

case.

While we are computing arrow lengths, each candidate answer will be the sum of the arrows in both directions from that node. We record these candidate answers and return the best one.

\n\n**Complexity Analysis**

- \n
- \n
Time Complexity: , where is the number of nodes in the tree. We process every node once.

\n \n - \n
Space Complexity: , where is the height of the tree. Our recursive call stack could be up to layers deep.

\n \n

\n

Analysis written by: @awice

\n