-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumPathSum.java
More file actions
28 lines (24 loc) · 1.04 KB
/
MaximumPathSum.java
File metadata and controls
28 lines (24 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package BinaryTree;
public class MaximumPathSum {
public static void main(String args[]) {
TreeNode root = new TreeNode(1);
root . left = new TreeNode(3);
root . left . left = new TreeNode(5);
root . left . left . left = new TreeNode(7);
root . right = new TreeNode(2);
root . right . right = new TreeNode(4);
root . right . right . right = new TreeNode(6);
int max[] = new int[1];
max[0] = Integer.MIN_VALUE;
maxPath(max,root);
System.out.println(max[0]);
}
public static int maxPath(int max[] , TreeNode root) // gives the path which will give the max value
{
if(root == null)return 0;
int leftSum = Math.max(0 , maxPath(max,root.left)); // 0 because suppose the sum is comming nefgative then it will ignore it
int rightSum = Math.max(0 , maxPath(max , root.right));
max[0] = Math.max(max[0],leftSum + root.data + rightSum);
return root.data + Math.max(leftSum , rightSum);
}
}