687. Longest Univalue Path LeetCode Solution

In this guide, you will get 687. Longest Univalue Path LeetCode Solution with the best time and space complexity. The solution to Longest Univalue Path problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Longest Univalue Path solution in C++
  4. Longest Univalue Path solution in Java
  5. Longest Univalue Path solution in Python
  6. Additional Resources
687. Longest Univalue Path LeetCode Solution image

Problem Statement of Longest Univalue Path

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:

Input: root = [5,4,5,1,1,null,5]
Output: 2
Explanation: The shown image shows that the longest path of the same value (i.e. 5).

Example 2:

Input: root = [1,4,5,4,4,null,5]
Output: 2
Explanation: The shown image shows that the longest path of the same value (i.e. 4).

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(h)

687. Longest Univalue Path LeetCode Solution in C++

class Solution {
 public:
  int longestUnivaluePath(TreeNode* root) {
    int ans = 0;
    longestUnivaluePathDownFrom(root, ans);
    return ans;
  }

 private:
  int longestUnivaluePathDownFrom(TreeNode* root, int& ans) {
    if (root == nullptr)
      return 0;

    const int l = longestUnivaluePathDownFrom(root->left, ans);
    const int r = longestUnivaluePathDownFrom(root->right, ans);
    const int arrowLeft =
        root->left && root->left->val == root->val ? l + 1 : 0;
    const int arrowRight =
        root->right && root->right->val == root->val ? r + 1 : 0;
    ans = max(ans, arrowLeft + arrowRight);
    return max(arrowLeft, arrowRight);
  }
};
/* code provided by PROGIEZ */

687. Longest Univalue Path LeetCode Solution in Java

class Solution {
  public int longestUnivaluePath(TreeNode root) {
    longestUnivaluePathDownFrom(root);
    return ans;
  }

  private int ans = 0;

  private int longestUnivaluePathDownFrom(TreeNode root) {
    if (root == null)
      return 0;

    final int l = longestUnivaluePathDownFrom(root.left);
    final int r = longestUnivaluePathDownFrom(root.right);
    final int arrowLeft = root.left != null && root.left.val == root.val ? l + 1 : 0;
    final int arrowRight = root.right != null && root.right.val == root.val ? r + 1 : 0;
    ans = Math.max(ans, arrowLeft + arrowRight);
    return Math.max(arrowLeft, arrowRight);
  }
}
// code provided by PROGIEZ

687. Longest Univalue Path LeetCode Solution in Python

class Solution:
  def longestUnivaluePath(self, root: TreeNode | None) -> int:
    ans = 0

    def longestUnivaluePathDownFrom(root: TreeNode | None) -> int:
      nonlocal ans
      if not root:
        return 0

      l = longestUnivaluePathDownFrom(root.left)
      r = longestUnivaluePathDownFrom(root.right)
      arrowLeft = l + 1 if root.left and root.left.val == root.val else 0
      arrowRight = r + 1 if root.right and root.right.val == root.val else 0
      ans = max(ans, arrowLeft + arrowRight)
      return max(arrowLeft, arrowRight)

    longestUnivaluePathDownFrom(root)
    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.