Find the maximum path sum between two leaves of a binary tree | GeeksforGeeks
Given a binary tree in which each node element contains a number. Find the maximum possible sum from one leaf node to another.
Given a binary tree in which each node element contains a number. Find the maximum possible sum from one leaf node to another.
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).
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.
http://algorithms.tutorialhorizon.com/given-a-binary-tree-find-the-maximum-path-sum-between-any-two-leaves/
public static int maxSoFar =0;
public int maxPathSum(Node root){
if(root!=null){
int leftS = maxPathSum(root.left);
int rightS = maxPathSum(root.right);
int sumCurrent;
if(leftS<0 && rightS<0){//??? it's not a leaf
sumCurrent = root.data;
}else{
sumCurrent = Math.max(leftS+rightS+root.data , Math.max(leftS, rightS));
}
// sumCurrent = Math.max(leftS+rightS+root.data , Math.max(leftS, rightS));
if(maxSoFar<sumCurrent){
maxSoFar = sumCurrent;
}
return Math.max(leftS,rightS)+root.data;
}
else return 0;
}
http://gohired.in/2015/05/maximum-path-sum-between-two-leaves/
- Now we will calculate the max path sum between two leaves node
- So our max path will be either on the left sub tree OR
- Our max path will be either on the right sub tree OR
- Our max path will have some part in left and some part in right and passes through through the root
- Take a variable say, “maxSoFar=0″ this will our final result.
- Do postOrder traversal, This will give you result from left and right subtree
- Now at each node calcuate sumCurrent =Max of (result of leftSubtree,result of RightSubtree, result of leftSubtree+result of RightSubtree + Root data)
- if(maxSoFar<sumCurrent) then maxSoFar = sumCurrent
- At each node return max(result of leftSubtree,result of RightSubtree)+root.data;
public static int maxSoFar =0;
public int maxPathSum(Node root){
if(root!=null){
int leftS = maxPathSum(root.left);
int rightS = maxPathSum(root.right);
int sumCurrent;
if(leftS<0 && rightS<0){//??? it's not a leaf
sumCurrent = root.data;
}else{
sumCurrent = Math.max(leftS+rightS+root.data , Math.max(leftS, rightS));
}
// sumCurrent = Math.max(leftS+rightS+root.data , Math.max(leftS, rightS));
if(maxSoFar<sumCurrent){
maxSoFar = sumCurrent;
}
return Math.max(leftS,rightS)+root.data;
}
else return 0;
}
http://gohired.in/2015/05/maximum-path-sum-between-two-leaves/
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)
Read full article from Find the maximum path sum between two leaves of a binary tree | GeeksforGeeks