Longest Univalue Path Problem


Description

LeetCode Problem 687.

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.

Example 1:

1
2
Input: root = [5,4,5,1,1,5]
Output: 2

Example 2:

1
2
Input: root = [1,4,5,4,4,5]
Output: 2

Constraints:

  • The number of nodes in the tree is in the range [0, 10^4].
  • -1000 <= Node.val <= 1000
  • The depth of the tree will not exceed 1000.


Sample C++ Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int longestPath(TreeNode* root, int& max_path) {
        if (root == NULL)
            return 0;
        int left_path = longestPath(root->left, max_path);
        int right_path = longestPath(root->right, max_path);
        if ((root->left != NULL) && (root->val == root->left->val)) {
            left_path ++;
        } else {
            left_path = 0;
        }
        if ((root->right != NULL) && (root->val == root->right->val)) {
            right_path ++;
        } else {
            right_path = 0;
        }
        max_path = max(max_path, left_path + right_path);
        return max(left_path, right_path);
    }
    
    int longestUnivaluePath(TreeNode* root) {
        int max_path = 0;
        int val = longestPath(root, max_path);
        return max_path;
    }
};




Related Posts

Vertical Order Traversal Of A Binary Tree Problem

LeetCode 987. Given the root of a binary tree, calculate...

Univalued Binary Tree Problem

LeetCode 965. A binary tree is uni-valued if every node...

Sum Of Distances In Tree Problem

LeetCode 834. There is an undirected connected tree with n...

Smallest Subtree With All The Deepest Nodes Problem

LeetCode 865. Given the root of a binary tree, the...

Smallest String Starting From Leaf Problem

LeetCode 988. You are given the root of a binary...

Similar String Groups Problem

LeetCode 839. Two strings Xand Yare similar if we can...