forked from Anjalijha12345/Java-Problem-Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeMaxPathSum.java
49 lines (40 loc) · 1.59 KB
/
BinaryTreeMaxPathSum.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class BinaryTreeMaxPathSum {
private int maxPathSum = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
findMaxPathSum(root);
return maxPathSum;
}
private int findMaxPathSum(TreeNode node) {
if (node == null) {
return 0;
}
//Calculate the maximum path sum for the left and right subtrees
int leftMax = Math.max(findMaxPathSum(node.left), 0);
int rightMax = Math.max(findMaxPathSum(node.right), 0);
//Calculate the maximum path sum passing through the current node
int currentMax = node.val + leftMax + rightMax;
//Update the overall maximum path sum
maxPathSum = Math.max(maxPathSum, currentMax);
//Return the maximum path sum that can be extended from this node to its parent
return node.val + Math.max(leftMax, rightMax);
}
public static void main(String[] args) {
TreeNode root = new TreeNode(10);
root.left = new TreeNode(2);
root.right = new TreeNode(10);
root.left.left = new TreeNode(20);
root.left.right = new TreeNode(1);
root.right.right = new TreeNode(-25);
root.right.right.left = new TreeNode(3);
root.right.right.right = new TreeNode(4);
BinaryTreeMaxPathSum solution = new BinaryTreeMaxPathSum();
int maxPathSum = solution.maxPathSum(root);
System.out.println("Maximum Path Sum: " + maxPathSum);
}
}