Created
December 10, 2022 14:06
-
-
Save Guiwoo/2a4ae4bf1b51bacffff66bdaa4796b9e to your computer and use it in GitHub Desktop.
1339. Maximum Product of Splitted Binary Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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