File tree 2 files changed +61
-1
lines changed
2 files changed +61
-1
lines changed Original file line number Diff line number Diff line change @@ -61,7 +61,7 @@ that, this property should be satisfied at every node in the tree.
61
61
* Left View of a Binary Tree----> [ C++] ( /Code/C++/left-view.cpp )
62
62
* Level Order Traversal in BST ----> [ C++] ( ) | [ Java] ( ) | [ Python] ( /Code/Python/level_order_traversal_binary_tree.py )
63
63
* 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 )
65
65
* Maximum Topology Short ----> [ C++] ( /Code/C++/Max_Topology_Short.cpp )
66
66
* Median of running streams of integers ----> [ C++] ( /Code/C++/median_running_stream.cpp )
67
67
* 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.
76
76
* Searching in BST ----> [ C++] ( /Code/C++/searching_in_bst.cpp ) | [ Java] ( Code\Java\Searching_in_BST.Java )
77
77
* Threaded Tree ----> [ C++] ( /Code/C++/threaded_binary_tree.cpp )
78
78
* Top-View of a Binary tree ----> [ C++] ( /Code/C++/Top-View.cpp )
79
+
Original file line number Diff line number Diff line change
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
+ */
You can’t perform that action at this time.
0 commit comments