package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/
 */
public class Problem1315 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

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

    public int sumEvenGrandparent(TreeNode root) {
        return sumEvenGrandparent(root, new ArrayList<>());
    }

    private static int sumEvenGrandparent(TreeNode root, List<Integer> grandparents) {
        if (root == null) {
            return 0;
        }
        int even = 0;
        if (grandparents.size() >= 2 && grandparents.get(grandparents.size() - 2) % 2 == 0) {
            even = root.val;
        }
        List<Integer> copy = new ArrayList<>(grandparents);
        copy.add(root.val);
        int left = sumEvenGrandparent(root.left, copy);
        copy = new ArrayList<>(grandparents);
        copy.add(root.val);
        int right = sumEvenGrandparent(root.right, copy);
        return even + left + right;
    }

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

@@ -1080,105 +1080,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1315 prob = new Problem1315();%0A    %7D
 %0A%7D%0A

Solve problem 1315