From cfa2f078ef7bbba38ba9930909c9d3f46e1829f5 Mon Sep 17 00:00:00 2001 From: shubhidua Date: Thu, 29 Apr 2021 21:15:46 +0530 Subject: [PATCH 1/3] Maximum Sum along any path in java --- maximumSum.java | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 maximumSum.java diff --git a/maximumSum.java b/maximumSum.java new file mode 100644 index 000000000..d239b4992 --- /dev/null +++ b/maximumSum.java @@ -0,0 +1,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); + +*/ From 9d06d04ace148811f74b0191d1fc235a4dd29d00 Mon Sep 17 00:00:00 2001 From: shubhidua Date: Sat, 29 May 2021 21:39:35 +0530 Subject: [PATCH 2/3] Updated readme file --- Tree/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tree/readme.md b/Tree/readme.md index e664a82dd..25e2c4e85 100644 --- a/Tree/readme.md +++ b/Tree/readme.md @@ -61,7 +61,7 @@ that, this property should be satisfied at every node in the tree. * Spiral Traversal of Binary Tree ----> [C++](/Code/C++/spiral_traversal_of_binary_tree.cpp) * Searching in BST ----> [C++](/Code/C++/searching_in_bst.cpp) | [Java](Code\Java\Searching_in_BST.Java) * Threaded Tree ----> [C++](/Code/C++/threaded_binary_tree.cpp) -* Maiximum sum path from any node to any node in Binary Tree ----> [C++](/Code/C++/max_tree_path.cpp) +* Maiximum sum path from any node to any node in Binary Tree ----> [C++](/Code/C++/max_tree_path.cpp) | [Java][/Code/Java/maximumSum.java] * Maximum Topology Short ----> [C++](/Code/C++/Max_Topology_Short.cpp) From f39fcb5ddd90921f55e4ea1d16065f2ae40e41b2 Mon Sep 17 00:00:00 2001 From: Rudrakshi Date: Sun, 30 May 2021 12:25:30 +0530 Subject: [PATCH 3/3] update --- Tree/readme.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Tree/readme.md b/Tree/readme.md index 08990d94d..44e31411f 100644 --- a/Tree/readme.md +++ b/Tree/readme.md @@ -61,8 +61,7 @@ that, this property should be satisfied at every node in the tree. * Left View of a Binary Tree----> [C++](/Code/C++/left-view.cpp) * Level Order Traversal in BST ----> [C++]() | [Java]() | [Python](/Code/Python/level_order_traversal_binary_tree.py) * Lowest common ancestor in a binary tree ----> [Python](/Code/Python/LCA_in_binary_tree.py) -* Maiximum sum path from any node to any node in Binary Tree ----> [C++](/Code/C++/max_tree_path.cpp) -* Maiximum sum path from any node to any node in Binary Tree ----> [Java][/Code/Java/maximumSum.java] +* Maiximum sum path from any node to any node in Binary Tree ----> [C++](/Code/C++/max_tree_path.cpp) | [Java](/Code/Java/maximumSum.java) * Maximum Topology Short ----> [C++](/Code/C++/Max_Topology_Short.cpp) * Median of running streams of integers ----> [C++](/Code/C++/median_running_stream.cpp) * N-ary Tree ----> [Python](/Code/Python/n_ary_tree.py) @@ -76,7 +75,5 @@ that, this property should be satisfied at every node in the tree. * Distance between two nodes of a binary tree ----> [C++](/Code/C++/distance_between_two_nodes_of_BT.cpp) * Searching in BST ----> [C++](/Code/C++/searching_in_bst.cpp) | [Java](Code\Java\Searching_in_BST.Java) * Threaded Tree ----> [C++](/Code/C++/threaded_binary_tree.cpp) -* Maiximum sum path from any node to any node in Binary Tree ----> [Java][/Code/Java/maximumSum.java] -* Maximum Topology Short ----> [C++](/Code/C++/Max_Topology_Short.cpp) * Top-View of a Binary tree ----> [C++](/Code/C++/Top-View.cpp)