Skip to content

Instantly share code, notes, and snippets.

@Guiwoo
Created December 10, 2022 14:06
Show Gist options
  • Save Guiwoo/2a4ae4bf1b51bacffff66bdaa4796b9e to your computer and use it in GitHub Desktop.
Save Guiwoo/2a4ae4bf1b51bacffff66bdaa4796b9e to your computer and use it in GitHub Desktop.
1339. Maximum Product of Splitted Binary Tree
class Solution {
int module = (int) Math.pow(10,9)+7;
public int maxProduct(TreeNode root) {
List<Long> sum = new ArrayList<>();
long total = getTotalSum(root,sum);
int max = 0;
for (Long target : sum) {
long t = (target * (total - target);
max = Math.max(max,t);
}
return (int) (max%module);
}
public long getTotalSum(TreeNode node, List<Long> sum){
if(node == null) return 0;
long left = getTotalSum(node.left,sum);
long right = getTotalSum(node.right,sum);
long total = (left + right)%module;
sum.add(total);
return total;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment