package leetcode;

/**
 * https://leetcode.com/problems/longest-univalue-path/
 */
public class Problem687 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public int longestUnivaluePath(TreeNode root) {
        if (root == null) {
            return 0;
        }
        IntRef intRef = new IntRef();
        longestUnivaluePath(root, intRef);
        return intRef.val - 1;
    }

    private static class IntRef {
        private int val;
    }

    private ValCount longestUnivaluePath(TreeNode root, IntRef ref) {
        if (root == null) {
            return null;
        }
        ValCount left = longestUnivaluePath(root.left, ref);
        ValCount right = longestUnivaluePath(root.right, ref);
        int count = 1;
        if (left != null) {
            if (root.val == left.val) {
                count += left.count;
            }
        }
        if (right != null) {
            if (root.val == right.val) {
                count += right.count;
            }
        }
        ref.val = Math.max(ref.val, count);
        return new ValCount(root.val, count);
    }

    private static class ValCount {
        private final int val;
        private final int count;

        public ValCount(int val, int count) {
            this.val = val;
            this.count = count;
        }
    }

    public static void main(String[] args) {
        Problem687 prob = new Problem687();

        TreeNode root = new TreeNode(5);
        root.left = new TreeNode(4);
        root.left.left = new TreeNode(1);
        root.left.right = new TreeNode(1);
        root.right = new TreeNode(5);
        root.right.right = new TreeNode(5);
        System.out.println(prob.longestUnivaluePath(root)); // 2

        root = new TreeNode(1);
        root.left = new TreeNode(4);
        root.left.left = new TreeNode(4);
        root.left.right = new TreeNode(4);
        root.right = new TreeNode(5);
        root.right.right = new TreeNode(5);
        System.out.println(prob.longestUnivaluePath(root)); // 2

        root = new TreeNode(1);
        System.out.println(prob.longestUnivaluePath(root)); // 0
    }
}

@@ -828,25 +828,30 @@
         int 
-c
+totalC
 ount = 1;%0A  
@@ -844,24 +844,50 @@
 lCount = 1;%0A
+        int maxCount = 0;%0A
         if (
@@ -958,24 +958,45 @@
         
-c
+maxC
 ount 
-+
 =
+ Math.max(maxCount,
  left.co
@@ -994,24 +994,25 @@
 , left.count
+)
 ;%0A          
@@ -1115,16 +1115,37 @@
     
-c
+maxC
 ount 
-+
 =
+ Math.max(maxCount,
  rig
@@ -1148,24 +1148,25 @@
  right.count
+)
 ;%0A          
@@ -1171,32 +1171,64 @@
     %7D%0A        %7D%0A
+        totalCount += maxCount;%0A
         ref.val 
@@ -1243,25 +1243,30 @@
 ax(ref.val, 
-c
+totalC
 ount);%0A     
@@ -1298,17 +1298,22 @@
 ot.val, 
-c
+totalC
 ount);%0A 

Update problem 687