Advertisement
sweet1cris

Untitled

Jan 9th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1. /**
  2.  * Definition of TreeNode:
  3.  * public class TreeNode {
  4.  *     public int val;
  5.  *     public TreeNode left, right;
  6.  *     public TreeNode(int val) {
  7.  *         this.val = val;
  8.  *         this.left = this.right = null;
  9.  *     }
  10.  * }
  11.  */
  12. public class Solution {
  13.     /**
  14.      * @param root the root of binary tree.
  15.      * @return an integer
  16.      */
  17.     public int maxPathSum2(TreeNode root) {
  18.         if (root == null) {
  19.             return Integer.MIN_VALUE;
  20.         }
  21.        
  22.         int left = maxPathSum2(root.left);
  23.         int right = maxPathSum2(root.right);
  24.         return root.val + Math.max(0, Math.max(left, right));
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement