Given a binary tree in which each node element contains a number. Find the maximum possible sum from one leaf node to another.
The maximum sum path may or may not go through root. For example, in the following binary tree, the maximum sum is 27(3 + 6 + 9 + 0 – 1 + 10). Expected time complexity is O(n).
If one side of root is empty, then function should return minus infinite (INT_MIN in case of C/C++)
A simple solution is to traverse the tree and do following for every traversed node X.
1) Find maximum sum from leaf to root in left subtree of X (we can use this post for this and next steps)
2) Find maximum sum from leaf to root in right subtree of X.
3) Add the above two calculated values and X->data and compare the sum with the maximum value obtained so far and update the maximum value.
4) Return the maximum value.
The time complexity of above solution is O(n2)
We can find the maximum sum using single traversal of binary tree. The idea is to maintain two values in recursive calls
1) Maximum root to leaf path sum for the subtree rooted under current node.
2) The maximum path sum between leaves (desired output).
For every visited node X, we find the maximum root to leaf sum in left and right subtrees of X. We add the two values with X->data, and compare the sum with maximum path sum found so far.
Following is the implementation of the above O(n) solution.
Max pathSum of the given binary tree is 27.
Thanks to Saurabh Vats for suggesting corrections in original approach.
This article is contributed by Kripal Gaurav. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Tree with N nodes and K leaves such that distance between farthest leaves is minimized
- Minimum sum path between two leaves of a binary tree
- Find sum of all left leaves in a given Binary Tree
- Find sum of all right leaves in a given Binary Tree
- Find first non matching leaves in two binary trees
- Extract Leaves of a Binary Tree in a Doubly Linked List
- Print all nodes in a binary tree having K leaves
- Height of binary tree considering even level leaves only
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Find the maximum sum leaf to root path in a Binary Tree
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Mimimum number of leaves required to be removed from a Tree to satisfy the given condition
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Shortest path between two nodes in array like representation of binary tree
- XOR of path between any two nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree | Set 2
- Maximum Path Sum in a Binary Tree
- Minimum count of Full Binary Trees such that the count of leaves is N
- Find all root to leaf path sum of a Binary Tree
Improved By : shrikanth13