Skip to content

Commit 350edc4

Browse files
Maximum Sum along any path in java (#1817)
* Maximum Sum along any path in java * Updated readme file * update Co-authored-by: Rudrakshi <[email protected]>
1 parent e1a2b44 commit 350edc4

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

Tree/readme.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ that, this property should be satisfied at every node in the tree.
6161
* Left View of a Binary Tree----> [C++](/Code/C++/left-view.cpp)
6262
* Level Order Traversal in BST ----> [C++]() | [Java]() | [Python](/Code/Python/level_order_traversal_binary_tree.py)
6363
* Lowest common ancestor in a binary tree ----> [Python](/Code/Python/LCA_in_binary_tree.py)
64-
* Maiximum sum path from any node to any node in Binary Tree ----> [C++](/Code/C++/max_tree_path.cpp)
64+
* Maiximum sum path from any node to any node in Binary Tree ----> [C++](/Code/C++/max_tree_path.cpp) | [Java](/Code/Java/maximumSum.java)
6565
* Maximum Topology Short ----> [C++](/Code/C++/Max_Topology_Short.cpp)
6666
* Median of running streams of integers ----> [C++](/Code/C++/median_running_stream.cpp)
6767
* N-ary Tree ----> [Python](/Code/Python/n_ary_tree.py)
@@ -76,3 +76,4 @@ that, this property should be satisfied at every node in the tree.
7676
* Searching in BST ----> [C++](/Code/C++/searching_in_bst.cpp) | [Java](Code\Java\Searching_in_BST.Java)
7777
* Threaded Tree ----> [C++](/Code/C++/threaded_binary_tree.cpp)
7878
* Top-View of a Binary tree ----> [C++](/Code/C++/Top-View.cpp)
79+

maximumSum.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Problem Statement-
3+
We need to find maximum sum path from any node to any node in a binary tree.
4+
5+
Algorithm-
6+
In order to check this we need to traverse through all the possible paths.
7+
We start traversing from the one node and we keep on storing the max result in a variable.
8+
9+
*/
10+
11+
import java.io.*;
12+
import java.util.*;
13+
14+
//Creating basic tree data structure
15+
class Node {
16+
int val;
17+
Node left,right;
18+
// constructor
19+
20+
Node(int data){
21+
val=data;
22+
left=null;
23+
right=null;
24+
}
25+
}
26+
27+
public class maximumSum {
28+
static int ans;
29+
public static void main(String[] args) throws IOException {
30+
Scanner sc=new Scanner(System.in);
31+
Node root=new Node(-10);
32+
root.left=new Node(9);
33+
root.right=new Node(20);
34+
root.right.left=new Node(15);
35+
root.right.right=new Node(7);
36+
37+
ans=Integer.MIN_VALUE;
38+
maxSum(root);
39+
System.out.println(ans);
40+
41+
sc.close();
42+
return;
43+
}
44+
45+
// An recursive function to find the maximum sum along any path.
46+
// Stores the result in a variable named as answer.
47+
public static int maxSum(Node root){
48+
if (root == null) return 0;
49+
int left = Math.max(0, maxSum(root.left));
50+
int right = Math.max(0, maxSum(root.right));
51+
ans = Math.max(ans, left + right +root.val);
52+
return Math.max(left, right) +root.val;
53+
}
54+
}
55+
/*
56+
Time Complexity-O(n);
57+
Space Complexity-O(n);
58+
59+
*/

0 commit comments

Comments
 (0)