Sum Root to Leaf Path Problem

You are given an arbitrary binary tree consisting of N nodes where each node is associated with an integer value from 1 to 9. Each root-to-leaf path in the tree represents a number formed by concatenating the values from root to leaf.

Example:

       1
      /  \
     2    3

The root to leaf path 1->2 represents the number 12.
The root to leaf path 1->3 represents the number 13.

Your task is to calculate the total sum of all the possible root-to-leaf paths in the tree.

Input:

1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1

Output:

25

Constraints:

  • 1 ≤ T ≤ 50 - the number of test cases.
  • 1 ≤ N ≤ 3000 - number of nodes in the tree.
  • 1 ≤ nodeValue ≤ 9 - node values are between 1 and 9.
Note:

The output may be very large, return the answer modulo (109 + 7).

Example Explanation:

The total sum of all the root-to-leaf paths is 12 (from path 1->2) + 13 (from path 1->3) = 25. Hence, the output is 25.

In the input, nodes are represented in level order, using -1 for null nodes.

AnswerBot
4mo

Calculate the total sum of all possible root-to-leaf paths in a binary tree.

  • Traverse the tree from root to leaf nodes, keeping track of the current path sum.

  • Add the path sum to the total sum when reac...read more

Help your peers!
Select
Add answer anonymously...

Josh Technology Group Software Developer Intern interview questions & answers

A Software Developer Intern was asked 3mo agoQ. Given a linked list, how do you ensure that it is in decreasing order?
A Software Developer Intern was asked 3mo agoQ. Given a random node in a BST, how do you check if a given element is part of the...read more
A Software Developer Intern was asked Q. Merge Two Binary Trees Problem Statement You are given the roots of two binary t...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 3mo agoQ1. Given a linked list, how do you ensure that it is in decreasing order?
A Software Developer Intern was asked 3mo agoQ2. Given a random node in a BST, how do you check if a given element is part of the...read more
A Software Developer Intern was asked Q3. Merge Two Binary Trees Problem Statement You are given the roots of two binary t...read more
Josh Technology Group Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits