-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaximumPathSum.java
More file actions
72 lines (60 loc) · 2.83 KB
/
MaximumPathSum.java
File metadata and controls
72 lines (60 loc) · 2.83 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//class TreeNode {
// int val;
// TreeNode left;
// TreeNode right;
//
// TreeNode(int x) {
// val = x;
// }
//};
class MaximumPathSum {
private static int globalMaximumSum;
public static int findMaximumPathSum(TreeNode root) {
globalMaximumSum = Integer.MIN_VALUE;
findMaximumPathSumRecursive(root);
return globalMaximumSum;
}
private static int findMaximumPathSumRecursive(TreeNode currentNode) {
if (currentNode == null)
return 0;
int maxPathSumFromLeft = findMaximumPathSumRecursive(currentNode.left);
int maxPathSumFromRight = findMaximumPathSumRecursive(currentNode.right);
// ignore paths with negative sums, since we need to find the maximum sum we should
// ignore any path which has an overall negative sum.
maxPathSumFromLeft = Math.max(maxPathSumFromLeft, 0);
maxPathSumFromRight = Math.max(maxPathSumFromRight, 0);
// maximum path sum at the current node will be equal to the sum from the left subtree +
// the sum from right subtree + val of current node
int localMaximumSum = maxPathSumFromLeft + maxPathSumFromRight + currentNode.val;
// update the global maximum sum
globalMaximumSum = Math.max(globalMaximumSum, localMaximumSum);
// maximum sum of any path from the current node will be equal to the maximum of
// the sums from left or right subtrees plus the value of the current node
return Math.max(maxPathSumFromLeft, maxPathSumFromRight) + currentNode.val;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
//System.out.println("Maximum Path Sum: " + MaximumPathSum.findMaximumPathSum(root));
root.left.left = new TreeNode(1);
root.left.right = new TreeNode(3);
root.right.left = new TreeNode(5);
root.right.right = new TreeNode(6);
root.right.left.left = new TreeNode(7);
root.right.left.right = new TreeNode(8);
root.right.right.left = new TreeNode(9);
System.out.println("Maximum Path Sum: " + MaximumPathSum.findMaximumPathSum(root));
root = new TreeNode(-1);
root.left = new TreeNode(-3);
System.out.println("Maximum Path Sum: " + MaximumPathSum.findMaximumPathSum(root));
}
}
// Time complexity#
// The time complexity of the above algorithm is O(N)
// O(N) , where ‘N’ is the total number of nodes in the tree. This is due to the fact that we traverse each node once.
//
// Space complexity#
// The space complexity of the above algorithm will be O(N)
// O(N) in the worst case. This space will be used to store the recursion stack.
// The worst case will happen when the given tree is a linked list (i.e., every node has only one child).