forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxSumPath.java
33 lines (30 loc) · 863 Bytes
/
MaxSumPath.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
/*https://practice.geeksforgeeks.org/problems/maximum-sum-leaf-to-root-path/1*/
class Solution
{
static int maxSum;
public static int maxPathSum(Node root)
{
//code here
maxSum = Integer.MIN_VALUE;
recur(root,0);
return maxSum;
}
public static void recur(Node root, int currSum)
{
if (root.left == null && root.right == null)
{
maxSum = Math.max(currSum+root.data, maxSum);
return;
}
if (root.left != null) recur(root.left,currSum+root.data);
if (root.right != null) recur(root.right,currSum+root.data);
}
}
class Solution {
public int solve(Tree root) {
if (root == null) return 0;
int leftSum = solve(root.left);
int rightSum = solve(root.right);
return Math.max(leftSum,rightSum)+root.val;
}
}