VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sum-numbers-formed-root-leaf-paths/

⇱ Sum of all the numbers that are formed from root to leaf paths - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sum of all the numbers that are formed from root to leaf paths

Last Updated : 23 Jul, 2025

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

Examples:

Input:

👁 Sum-of-all-the-numbers-that-are-formed-from-root-to-leaf-paths-48

Output: 13997
Explanation: There are 4 leaves, hence 4 root to leaf paths:

  • 6->3->2 = 632
  • 6->3->5->7 = 6357
  • 6->3->5->4 = 6354
  • 6->5->4 = 654

final answer = 632 + 6357 + 6354 + 654 = 13997

Input:

👁 sum-of-all-the-numbers-that-are-formed-from-root-to-leaf-paths

Output: 2630
Explanation: There are 3 leaves, resulting in leaf path of 1240, 1260, 130 sums to 2630.

The Naive approach to solve this problem is to first find all the paths from the root to the leaf . Then we convert all paths into numbers. In the end, we will add those numbers. Time Complexity of this approach will be O(n^2)  because we are traversing the all the paths and space will be O(n).

[Expected Approach] Using Pre-order traversal - O(n) Time and O(h) Space

The idea is to do a preorder traversal of the tree. In the preorder traversal, keep track of the value calculated till the current node. For every node, we update the value as value*10 plus the node's data. On reaching a leaf node, return the value calculated so far.


Output
13997

[Alternate Approach] Using Iterative Method - O(n) Time and O(h) Space

The idea behind the Iterative Depth-First Search (DFS) using a Stack approach is to traverse the binary tree iteratively in a depth-first manner while keeping track of the path sum from the root to the current node. We use a stack to store pairs of nodes and their corresponding path sums.

Step by step approach:

  • Start with an initial sum value of 0 and create an empty stack.
  • Push the root node onto the stack along with its value as the initial path sum.
  • While the stack is not empty, do the following:
    • Pop a node and its corresponding path sum from the stack.
    • If the popped node is a leaf (i.e., it has no left and right children), add the path sum to the overall result.
    • If the popped node has a right child, push the right child onto the stack with the updated path sum. The updated path sum is obtained by multiplying the current path sum by 10 and adding the value of the right child.
    • If the popped node has a left child, push the left child onto the stack with the updated path sum. The updated path sum is obtained by multiplying the current path sum by 10 and adding the value of the left child.
  • Once the stack is empty, return the overall result, which represents the sum of all the numbers formed from root to leaf paths.

Below is the implementation of the above approach:


Output
13997
Comment
Article Tags:
Article Tags: