Maximum Product of Splitted Binary Tree

给一个二叉树, 通过断其中一个edge, 求断后两个子树的值的和的乘积最大. 这个题我开始的时候, 是扫了两边做的, 后来发现用一个set可以直接扫一次, set中装满所有的子树和…好吧…test cases不是那么紧.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxProduct(TreeNode root) {
        Set<Integer> sums = new HashSet<>();
        int total = find(root, sums);
        long max = 0;
        for (long sum : sums) {
            max = Math.max(max, sum * (total - sum));
        }
        return (int) (max % (1e9+7));
    }

    public int find(TreeNode root, Set<Integer> sums) {
        if (root == null)
            return 0;
        int sum = find(root.left, sums) + find(root.right, sums) + root.val;
        sums.add(sum);
        return sum;
    }
}