[Approach]Using Recursion - O(n) Time and O(h) Recursive Space
The idea is to use postorder traversal. At each node, calculate left and right path sums, and update a global maximum with (left + node + right). Return the node’s value plus the larger side upward. The global maximum at the end gives the answer.
Why this works: Any maximum path in a binary tree must pass through some "highest" node (its root in that path). By considering every node as a possible highest point and updating the maximum with left + node + right, we guarantee that the best path is captured. Returning only one side ensures valid extension toward the parent without breaking the path structure.
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*left,*right;// Constructor to initialize a new nodeNode(intvalue){data=value;left=nullptr;right=nullptr;}};// Returns the maximum path // sum in the subtree with the current node as an endpoint. intfindMaxSumRec(Node*root,int&res){if(root==NULL)return0;// Calculate maximum path sums for left and right subtreesintl=max(0,findMaxSumRec(root->left,res));intr=max(0,findMaxSumRec(root->right,res));// Update 'res' with the maximum path// sum passing through the current noderes=max(res,l+r+root->data);returnroot->data+max(l,r);}// Returns maximum path sum in tree with given rootintfindMaxSum(Node*root){intres=root->data;// Compute maximum path sum and store it in 'res'findMaxSumRec(root,res);returnres;}intmain(){// Representation of input binary tree:// 10// / \ // 2 10// / \ \ // 20 1 -25// / \ // 3 4Node*root=newNode(10);root->left=newNode(2);root->right=newNode(10);root->left->left=newNode(20);root->left->right=newNode(1);root->right->right=newNode(-25);root->right->right->left=newNode(3);root->right->right->right=newNode(4);cout<<findMaxSum(root);return0;}
Java
classNode{intdata;Nodeleft,right;// Constructor to initialize a new nodeNode(intvalue){data=value;left=null;right=null;}}classGFG{staticintfindMaxSumRec(Noderoot,int[]res){if(root==null)return0;// Calculate maximum path sums for left and right subtreesintl=Math.max(0,findMaxSumRec(root.left,res));intr=Math.max(0,findMaxSumRec(root.right,res));// Update 'res' with the maximum path// sum passing through the current noderes[0]=Math.max(res[0],l+r+root.data);returnroot.data+Math.max(l,r);}// Returns maximum path sum in tree with given rootstaticintfindMaxSum(Noderoot){int[]res={root.data};// Compute maximum path sum and store it in 'res'findMaxSumRec(root,res);returnres[0];}publicstaticvoidmain(String[]args){// Representation of input binary tree:// 10// / \// 2 10// / \ \ // 20 1 -25// / \// 3 4Noderoot=newNode(10);root.left=newNode(2);root.right=newNode(10);root.left.left=newNode(20);root.left.right=newNode(1);root.right.right=newNode(-25);root.right.right.left=newNode(3);root.right.right.right=newNode(4);System.out.println(findMaxSum(root));}}
Python
classNode:def__init__(self,value):self.data=valueself.left=Noneself.right=None# Returns the maximum path #sum in the subtree with the current node as an endpoint. deffindMaxSumRec(root,res):ifrootisNone:return0# Calculate maximum path sums for left and right subtreesl=max(0,findMaxSumRec(root.left,res))r=max(0,findMaxSumRec(root.right,res))# Update 'res' with the maximum path#sum passing through the current noderes[0]=max(res[0],l+r+root.data)returnroot.data+max(l,r)# Returns maximum path sum in tree with given rootdeffindMaxSum(root):res=[root.data]# Compute maximum path sum and store it in 'res'findMaxSumRec(root,res)returnres[0]if__name__=="__main__":# Representation of input binary tree:# 10# / \# 2 10# / \ \ # 20 1 -25# / \# 3 4root=Node(10)root.left=Node(2)root.right=Node(10)root.left.left=Node(20)root.left.right=Node(1)root.right.right=Node(-25)root.right.right.left=Node(3)root.right.right.right=Node(4)print(findMaxSum(root))
C#
usingSystem;classNode{publicintdata;publicNodeleft,right;// Constructor to initialize a new nodepublicNode(intvalue){data=value;left=null;right=null;}}classGFG{// Returns the maximum path //sum in the subtree with the current node as an endpoint. staticintfindMaxSumRec(Noderoot,refintres){if(root==null)return0;// Calculate maximum path sums for left and right subtreesintl=Math.Max(0,findMaxSumRec(root.left,refres));intr=Math.Max(0,findMaxSumRec(root.right,refres));// Update 'res' with the maximum path//sum passing through the current noderes=Math.Max(res,l+r+root.data);returnroot.data+Math.Max(l,r);}// Returns maximum path sum in tree with given rootstaticintfindMaxSum(Noderoot){intres=root.data;// Compute maximum path sum and store it in 'res'findMaxSumRec(root,refres);returnres;}staticvoidMain(string[]args){// Representation of input binary tree:// 10// / \// 2 10// / \ \ // 20 1 -25// / \// 3 4Noderoot=newNode(10);root.left=newNode(2);root.right=newNode(10);root.left.left=newNode(20);root.left.right=newNode(1);root.right.right=newNode(-25);root.right.right.left=newNode(3);root.right.right.right=newNode(4);Console.WriteLine(findMaxSum(root));}}
JavaScript
classNode{constructor(value){this.data=value;this.left=null;this.right=null;}}// Returns the maximum path //sum in the subtree with the current node as an endpoint. functionfindMaxSumRec(root,res){if(root===null)return0;// Calculate maximum path sums for left and right subtreesletl=Math.max(0,findMaxSumRec(root.left,res));letr=Math.max(0,findMaxSumRec(root.right,res));// Update 'res' with the maximum path//sum passing through the current noderes.val=Math.max(res.val,l+r+root.data);returnroot.data+Math.max(l,r);}// Returns maximum path sum in tree with given rootfunctionfindMaxSum(root){letres={val:root.data};// Compute maximum path sum and store it in 'res'findMaxSumRec(root,res);returnres.val;}// Representation of input binary tree:// 10// / \// 2 10// / \ \ // 20 1 -25// / \// 3 4letroot=newNode(10);root.left=newNode(2);root.right=newNode(10);root.left.left=newNode(20);root.left.right=newNode(1);root.right.right=newNode(-25);root.right.right.left=newNode(3);root.right.right.right=newNode(4);console.log(findMaxSum(root));