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) {
        // TODO
        return 0;
    }

    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
    }
}

@@ -330,33 +330,1099 @@
     
-// TODO%0A        return 0;
+if (root == null) %7B%0A            return 0;%0A        %7D%0A        IntRef intRef = new IntRef();%0A        longestUnivaluePath(root, intRef);%0A        return intRef.val - 1;%0A    %7D%0A%0A    private static class IntRef %7B%0A        private int val;%0A    %7D%0A%0A    private ValCount longestUnivaluePath(TreeNode root, IntRef ref) %7B%0A        if (root == null) %7B%0A            return null;%0A        %7D%0A        ValCount left = longestUnivaluePath(root.left, ref);%0A        ValCount right = longestUnivaluePath(root.right, ref);%0A        int count = 1;%0A        if (left != null) %7B%0A            if (root.val == left.val) %7B%0A                count += left.count;%0A            %7D%0A        %7D%0A        if (right != null) %7B%0A            if (root.val == right.val) %7B%0A                count += right.count;%0A            %7D%0A        %7D%0A        ref.val = Math.max(ref.val, count);%0A        return new ValCount(root.val, count);%0A    %7D%0A%0A    private static class ValCount %7B%0A        private final int val;%0A        private final int count;%0A%0A        public ValCount(int val, int count) %7B%0A            this.val = val;%0A            this.count = count;%0A        %7D
 %0A   
@@ -2054,32 +2054,32 @@
 ew TreeNode(5);%0A
-
         System.o
@@ -2126,16 +2126,114 @@
 )); // 2
+%0A%0A        root = new TreeNode(1);%0A        System.out.println(prob.longestUnivaluePath(root)); // 0
 %0A    %7D%0A%7D

Update oroblem 687