Hello @aryamaan1011,
For finding the maximum sum path, you are performing four computations in each function call:
- return_val_left_subtree + root->data + return_val_right_subtree
- return_val_left_subtree + root->data
- root->data + return_val_right_subtree
- root->data
But, while returning a value, you have to compare only 2, 3 and 4th.
Reason:
Let us consider an example:
_____________ 1
_______2
____3 ______20
When you are at 2, maximum sum path will have value 25 for point 1.
But, when you will return a value for the parent, you have to consider one of the two possible paths i.e.
either 2->3 or 2->20
reason:
Because 1 cannot have both 3 and 20 in the same path.
So, you choose to return the path with maximum sum.
Also for the cases like:
_____________ 1
_______2
____-3 ______-20
You will return root->data
Finally, you return max(2.,3.,4.)
Hope, this would help.
Give a like if you are satisfied.