Geeksforgeeks

Error

×

Leaderboard

Showing:

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

@Ibrahim Nash | 6420 |

@blackshadows | 6380 |

@mb1973 | 5642 |

@Quandray | 5233 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4610 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3413 |

Complete Leaderboard | |

Handle | Score |

@sanskar94511 | 603 |

@akashverma4 | 363 |

@swastik8750 | 300 |

@nishunishuraj18 | 286 |

@surajmamgai | 264 |

@anks9013 | 222 |

@nayan2709 | 213 |

@burhanuddinbohra72 | 212 |

@realname | 203 |

@amank8975 | 203 |

@manmeetkaur18102000 | 202 |

Complete Leaderboard |

Given a binary tree of N nodes, where every node value is a number. Find the sum of all the numbers which are formed from root to leaf paths.

**Example 1:**

Input :6 / \ 3 5 / \ \ 2 5 4 / \ 7 4Output:13997Explanation :There are 4 leaves, hence 4 root to leaf paths: Path Number 6->3->2 632 6->3->5->7 6357 6->3->5->4 6354 6->5>4 654 Answer = 632 + 6357 + 6354 + 654 = 13997

**Example 2:**

Input :10 / \ 20 30 / \ 40 60Output :2630Explanation:Path Number 10->20->40 1240 10->20->60 1260 10->30 130 1240+1260+130 = 2630

**Your Task: **

You dont need to read input or print anything. Complete the function **treePathsSum()** which takes root node as input parameter and returns the sum of all the numbers formed by the root to leaf paths in the given Binary Tree.

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

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

**Constraints:**

1 ≤ N ≤ 12

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?

Yes
Root to leaf paths sum

...