Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    687 - Longest Univalue Path\.

    Medium

    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 <code>0, 10<sup>4</sup></code>.

    • -1000 &lt;= Node.val &lt;= 1000

    • The depth of the tree will not exceed 1000.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer longestUnivaluePath(TreeNode root)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait