Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6560 |

@blackshadows | 6400 |

@mb1973 | 5777 |

@Quandray | 5275 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5074 |

@kirtidee18 | 4408 |

@sanjay05 | 3762 |

@mantu_singh | 3666 |

@gfgaccount | 3605 |

@marius_valentin_dragoi | 3525 |

Complete Leaderboard | |

Handle | Score |

@anish5256 | 991 |

@vimleshpratapsingh321 | 980 |

@skj7 | 953 |

@kanewilliamson123 | 894 |

@pradeeppatidar1999 | 868 |

@akhileshkumar562002 | 862 |

@wallflowe101 | 826 |

@pd420786 | 813 |

@infilooop | 800 |

@bunnybug320 | 796 |

@nikhilkushwah280 | 796 |

Complete Leaderboard |

Given a binary tree in which each node element contains a number. Find the maximum possible sum from one leaf node to another leaf node.

**NOTE: **Here Leaf node is a node which is connected to exactly one different node.

**Example 1:**

Input :3 / \ 4 5 / \ -10 4Output:16Explanation :Maximum Sum lies between leaf node 4 and 5. 4 + 4 + 3 + 5 = 16.

**Example 2:**

Input :-15 / \ 5 6 / \ / \ -8 1 3 9 / \ \ 2 -3 0 / \ 4 -1 / 10Output :27Explanation:The maximum possible sum from one leaf node to another is (3 + 6 + 9 + 0 + -1 + 10 = 27)

**Your Task: **

You dont need to read input or print anything. Complete the function **maxPathSum() **which takes root node as input parameter and returns the maximum sum between 2 leaf nodes.

**Expected Time Complexity:** O(N)

**Expected Auxiliary Space:** O(Height of Tree)

2 ≤ Number of nodes ≤ 10

-10^{3 }≤ Value of each node ≤ 10^{3}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Maximum Path Sum between 2 Leaf Nodes

...