package leetcode;

/**
 * https://leetcode.com/problems/distribute-coins-in-binary-tree/
 */
public class Problem979 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

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

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

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

        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(0);
        root.right = new TreeNode(0);
        System.out.println(prob.distributeCoins(root)); // 2

        root = new TreeNode(5);
        root.left = new TreeNode(0);
        root.left.left = new TreeNode(0);
        root.left.right = new TreeNode(0);
        root.right = new TreeNode(0);
        System.out.println(prob.distributeCoins(root)); // 6

        root = new TreeNode(0);
        root.left = new TreeNode(3);
        root.right = new TreeNode(0);
        System.out.println(prob.distributeCoins(root)); // 3

        root = new TreeNode(1);
        root.left = new TreeNode(0);
        root.right = new TreeNode(2);
        System.out.println(prob.distributeCoins(root)); // 2

        root = new TreeNode(1);
        root.left = new TreeNode(0);
        root.left.right = new TreeNode(3);
        root.right = new TreeNode(0);
        System.out.println(prob.distributeCoins(root)); // 4
    }
}

@@ -336,594 +336,235 @@
     
-// TODO%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem979 prob = new Problem979();%0A%0A        TreeNode root = new TreeNode(3);%0A        root.left = new TreeNode(0);%0A        root.right = new TreeNode(0);%0A        System.out.println(prob.distributeCoins(root)); // 2%0A%0A        root = new TreeNode(5);%0A        root.left = new TreeNode(0)
+IntRef ref = new IntRef();%0A        distributeCoins(root, ref);%0A        return ref.val
 ;%0A    
+%7D%0A%0A
     
-root.left.left = new TreeNode(0);%0A        root.left.right = new TreeNode(0)
+private static class IntRef %7B%0A        private int val
 ;%0A    
+%7D%0A%0A
     
-root.right = new TreeNode(0);%0A        System.out.println(prob.distributeCoins(root)); // 6%0A%0A        root = new TreeNode(0);
+private static int distributeCoins(TreeNode root, IntRef ref) %7B
 %0A   
@@ -572,516 +572,241 @@
     
-root.lef
+if (roo
 t =
+=
  n
-ew TreeNode(3);%0A        root.right = new TreeNode(0);%0A        System.out.println(prob.distributeCoins(root)); // 3%0A%0A        root = new TreeNode(1);%0A        root.left = new TreeNode(0);%0A        root.right = new TreeNode(2);%0A        System.out.println(prob.distributeCoins(root)); // 2%0A%0A        root = new TreeNode(1);%0A        root.left = new TreeNode(0);%0A        root.left.right = new TreeNode(3);%0A        root.right = new TreeNode(0);%0A        System.out.println(prob.distributeCoins(root)); // 4
+ull) %7B%0A            return 0;%0A        %7D%0A        int a = distributeCoins(root.left, ref);%0A        int b = distributeCoins(root.right, ref);%0A        ref.val += Math.abs(a) + Math.abs(b);%0A        return root.val + a + b - 1;
 %0A   

Solve problem 979