package leetcode;

/**
 * https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/
 */
public class Problem1026 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

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

    public int maxAncestorDiff(TreeNode root) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1026 prob = new Problem1026();
        TreeNode root = new TreeNode(8);
        root.left = new TreeNode(3);
        root.left.left = new TreeNode(1);
        root.left.right = new TreeNode(6);
        root.left.right.left = new TreeNode(4);
        root.left.right.right = new TreeNode(7);
        root.right = new TreeNode(10);
        root.right.right = new TreeNode(14);
        root.right.right.left = new TreeNode(13);
        System.out.println(prob.maxAncestorDiff(root)); // 7
    }
}

@@ -350,15 +350,76 @@
     
-// TODO
+IntRef answer = new IntRef();%0A        maxAncestorDiff(root, answer);
 %0A   
@@ -430,17 +430,26 @@
  return 
-0
+answer.val
 ;%0A    %7D%0A
@@ -454,29 +454,30 @@
 %7D%0A%0A    p
-ublic
+rivate
  static 
 void mai
@@ -472,377 +472,1028 @@
 tic 
-void main(String%5B%5D args) %7B%0A        Problem1026 prob = new Problem1026();%0A        TreeNode root = new TreeNode(8)
+class MinMax %7B%0A        private int min;%0A        private int max;%0A%0A        public MinMax(int min, int max) %7B%0A            this.min = min;%0A            this.max = max;%0A        %7D%0A    %7D%0A%0A    private static class IntRef %7B%0A        private int val
 ;%0A    
+%7D%0A%0A
     
-root.left = new TreeNode(3);%0A        root.left.left = new TreeNode(1);%0A        root.left.right = new TreeNode(6);%0A        root.left.right.left = new TreeNode(4);%0A        root.left.right.
+private static MinMax maxAncestorDiff(TreeNode root, IntRef max) %7B%0A        if (root == null) %7B%0A            return null;%0A        %7D%0A        MinMax left = maxAncestorDiff(root.left, max);%0A        MinMax right = maxAncestorDiff(root.right, max);%0A        MinMax minMax = new MinMax(Integer.MAX_VALUE, Integer.MIN_VALUE);%0A        if (left != null) %7B%0A            minMax.min = Math.min(minMax.min, left.min);%0A            minMax.max = Math.max(minMax.max, left.max);%0A            int a = Math.abs(root.val - left.max);%0A            int b = Math.abs(root.val - left.min);%0A            max.val = Math.max(max.val, Math.max(a, b));%0A        %7D%0A        if (
 right 
+!
 = n
-ew TreeNode(7);%0A        root.right = new TreeNode(10
+ull) %7B%0A            minMax.min = Math.min(minMax.min, right.min);%0A            minMax.max = Math.max(minMax.max, right.max
 );%0A 
@@ -1503,42 +1503,49 @@
     
+  
-root.right.right = new TreeNode(14
+  int a = Math.abs(root.val - right.max
 );%0A 
@@ -1555,110 +1555,247 @@
     
+ 
-root.right.right.left = new TreeNode(13);%0A        System.out.println(prob.maxAncestorDiff(root)); // 7
+   int b = Math.abs(root.val - right.min);%0A            max.val = Math.max(max.val, Math.max(a, b));%0A        %7D%0A        minMax.min = Math.min(minMax.min, root.val);%0A        minMax.max = Math.max(minMax.max, root.val);%0A        return minMax;
 %0A   

Solve problem 1026