Advertisement
md5kafka

Untitled

Mar 10th, 2023
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. class Solution {
  2.     public int maxProduct(TreeNode root) {
  3.         if(root == null){
  4.             return 0;
  5.         }
  6.         return Math.max(sum(root.left, root.val), sum(root.right, root.val));
  7.     }
  8.  
  9.     public int sum(TreeNode root, int sumAbove) {
  10.         if(root == null) {
  11.             return 0;
  12.         }
  13.         int left = sum(root.left, root.val);
  14.         int right = sum(root.right, root.val);
  15.         int max = Math.max((left+sumAbove) * (right), (right+sumAbove) * (left));
  16.         max = Math.max(max, root.val);
  17.         return max;
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement