Maximum Path Sum in a Binary Tree (original) (raw)
Last Updated : 7 Oct, 2025
Given the **root of the binary tree, Find the **maximum path sum. The path may start and end at any node in the tree.
**Example:
**Input:
**Output: 42
**Explanation: Max path sum is represented using green color nodes in the above binary tree.
**Input:
**Output: 31
**Explanation: Max path sum is represented using green color nodes in the above binary tree.
[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 using namespace std;
class Node { public: int data; Node *left, *right;
// Constructor to initialize a new node
Node(int value) {
data = value;
left = nullptr;
right = nullptr;
}};
// Returns the maximum path // sum in the subtree with the current node as an endpoint. int findMaxSumRec(Node* root, int& res) {
if (root == NULL)
return 0;
// Calculate maximum path sums for left and right subtrees
int l = max(0, findMaxSumRec(root->left, res));
int r = max(0, findMaxSumRec(root->right, res));
// Update 'res' with the maximum path
// sum passing through the current node
res = max(res, l + r + root->data);
return root->data + max(l, r);}
// Returns maximum path sum in tree with given root int findMaxSum(Node* root) { int res = root->data;
// Compute maximum path sum and store it in 'res'
findMaxSumRec(root, res);
return res;}
int main() {
// Representation of input binary tree:
// 10
// / \
// 2 10
// / \ \
// 20 1 -25
// / \
// 3 4
Node* root = new Node(10);
root->left = new Node(2);
root->right = new Node(10);
root->left->left = new Node(20);
root->left->right = new Node(1);
root->right->right = new Node(-25);
root->right->right->left = new Node(3);
root->right->right->right = new Node(4);
cout << findMaxSum(root);
return 0;}
Java
class Node { int data; Node left, right;
// Constructor to initialize a new node
Node(int value) {
data = value;
left = null;
right = null;
}}
class GFG { static int findMaxSumRec(Node root, int[] res) {
if (root == null)
return 0;
// Calculate maximum path sums for left and right subtrees
int l = Math.max(0, findMaxSumRec(root.left, res));
int r = Math.max(0, findMaxSumRec(root.right, res));
// Update 'res' with the maximum path
// sum passing through the current node
res[0] = Math.max(res[0], l + r + root.data);
return root.data + Math.max(l, r);
}
// Returns maximum path sum in tree with given root
static int findMaxSum(Node root) {
int[] res = {root.data};
// Compute maximum path sum and store it in 'res'
findMaxSumRec(root, res);
return res[0];
}
public static void main(String[] args) {
// Representation of input binary tree:
// 10
// / \
// 2 10
// / \ \
// 20 1 -25
// / \
// 3 4
Node root = new Node(10);
root.left = new Node(2);
root.right = new Node(10);
root.left.left = new Node(20);
root.left.right = new Node(1);
root.right.right = new Node(-25);
root.right.right.left = new Node(3);
root.right.right.right = new Node(4);
System.out.println(findMaxSum(root));
}}
Python
class Node: def init(self, value): self.data = value self.left = None self.right = None
Returns the maximum path
#sum in the subtree with the current node as an endpoint. def findMaxSumRec(root, res):
if root is None:
return 0
# Calculate maximum path sums for left and right subtrees
l = max(0, findMaxSumRec(root.left, res))
r = max(0, findMaxSumRec(root.right, res))
# Update 'res' with the maximum path
#sum passing through the current node
res[0] = max(res[0], l + r + root.data)
return root.data + max(l, r)Returns maximum path sum in tree with given root
def findMaxSum(root): res = [root.data]
# Compute maximum path sum and store it in 'res'
findMaxSumRec(root, res)
return res[0]if name == "main":
# Representation of input binary tree:
# 10
# / \
# 2 10
# / \ \
# 20 1 -25
# / \
# 3 4
root = 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#
using System;
class Node { public int data; public Node left, right;
// Constructor to initialize a new node
public Node(int value) {
data = value;
left = null;
right = null;
}}
class GFG {
// Returns the maximum path
//sum in the subtree with the current node as an endpoint.
static int findMaxSumRec(Node root, ref int res) {
if (root == null)
return 0;
// Calculate maximum path sums for left and right subtrees
int l = Math.Max(0, findMaxSumRec(root.left, ref res));
int r = Math.Max(0, findMaxSumRec(root.right, ref res));
// Update 'res' with the maximum path
//sum passing through the current node
res = Math.Max(res, l + r + root.data);
return root.data + Math.Max(l, r);
}
// Returns maximum path sum in tree with given root
static int findMaxSum(Node root) {
int res = root.data;
// Compute maximum path sum and store it in 'res'
findMaxSumRec(root, ref res);
return res;
}
static void Main(string[] args) {
// Representation of input binary tree:
// 10
// / \
// 2 10
// / \ \
// 20 1 -25
// / \
// 3 4
Node root = new Node(10);
root.left = new Node(2);
root.right = new Node(10);
root.left.left = new Node(20);
root.left.right = new Node(1);
root.right.right = new Node(-25);
root.right.right.left = new Node(3);
root.right.right.right = new Node(4);
Console.WriteLine(findMaxSum(root));
}}
JavaScript
class Node { 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. function findMaxSumRec(root, res) {
if (root === null)
return 0;
// Calculate maximum path sums for left and right subtrees
let l = Math.max(0, findMaxSumRec(root.left, res));
let r = Math.max(0, findMaxSumRec(root.right, res));
// Update 'res' with the maximum path
//sum passing through the current node
res.val = Math.max(res.val, l + r + root.data);
return root.data + Math.max(l, r);}
// Returns maximum path sum in tree with given root function findMaxSum(root) { let res = { val: root.data };
// Compute maximum path sum and store it in 'res'
findMaxSumRec(root, res);
return res.val;}
// Representation of input binary tree:
// 10
// /
// 2 10
// / \ \
// 20 1 -25
// /
// 3 4
let root = new Node(10);
root.left = new Node(2);
root.right = new Node(10);
root.left.left = new Node(20);
root.left.right = new Node(1);
root.right.right = new Node(-25);
root.right.right.left = new Node(3);
root.right.right.right = new Node(4);
console.log(findMaxSum(root));
`



