Advertisement
sweet1cris

Untitled

Sep 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. public class MaxPathSumBinaryTreeIII {
  2.   public int maxPathSum(TreeNode root) {
  3.     // Assumptions: root is not null.
  4.     int[] max = new int[] { Integer.MIN_VALUE };
  5.     helper(root, max);
  6.     return max[0];
  7.   }
  8.  
  9.   private int helper(TreeNode root, int[] max) {
  10.     if (root == null) {
  11.       return 0;
  12.     }
  13.     int left = helper(root.left, max);
  14.     int right = helper(root.right, max);
  15.     int sin = Math.max(Math.max(left, right), 0) + root.key;
  16.     max[0] = Math.max(max[0], sin);
  17.     return sin;
  18.   }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement