Given a non-empty binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
publicclassSolution {int max =Integer.MIN_VALUE;publicintmaxPathSum(TreeNode root) {helper(root);return max; }// helper returns the max branch// plus current node's valueinthelper(TreeNode root) {if (root ==null)return0;// Not considering negative sum in our totalint left =Math.max(helper(root.left),0);int right =Math.max(helper(root.right),0); max =Math.max(max,root.val+ left + right);returnroot.val+Math.max(left, right); }}