-
Notifications
You must be signed in to change notification settings - Fork 618
/
maximumSum.java
59 lines (49 loc) · 1.36 KB
/
maximumSum.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
50
51
52
53
54
55
56
57
58
59
/*
Problem Statement-
We need to find maximum sum path from any node to any node in a binary tree.
Algorithm-
In order to check this we need to traverse through all the possible paths.
We start traversing from the one node and we keep on storing the max result in a variable.
*/
import java.io.*;
import java.util.*;
//Creating basic tree data structure
class Node {
int val;
Node left,right;
// constructor
Node(int data){
val=data;
left=null;
right=null;
}
}
public class maximumSum {
static int ans;
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
Node root=new Node(-10);
root.left=new Node(9);
root.right=new Node(20);
root.right.left=new Node(15);
root.right.right=new Node(7);
ans=Integer.MIN_VALUE;
maxSum(root);
System.out.println(ans);
sc.close();
return;
}
// An recursive function to find the maximum sum along any path.
// Stores the result in a variable named as answer.
public static int maxSum(Node root){
if (root == null) return 0;
int left = Math.max(0, maxSum(root.left));
int right = Math.max(0, maxSum(root.right));
ans = Math.max(ans, left + right +root.val);
return Math.max(left, right) +root.val;
}
}
/*
Time Complexity-O(n);
Space Complexity-O(n);
*/