Sum Root To Leaf Numbers

To solve this coding challenge, you need to traverse a binary tree and calculate the sum of all numbers formed by the paths from the root to the leaf nodes. Each path will represent a number which is formed by concatenating the values of the nodes from the root to the leaf. The sum of all these numbers is the solution.
# Explanation
The goal is to sum up all the numbers formed by the paths from the root to leaf nodes. We can achieve this by using a Depth-First Search (DFS) approach, which explores each root-to-leaf path one by one.
  1. Initialization and Class Definition:
    • First, initialize the basic structure of the node (TreeNode) which contains the value, a pointer to the left child, and a pointer to the right child.
    • Define the main solution class that contains the method
      sumNumbers
      .
  2. Helper Function (DFS):
    • Create a helper function
      dfs
      to traverse the tree.
    • This function takes two parameters: the current node and the current sum formed by the path.
    • The current sum needs to be updated as we traverse down the tree.
  3. Base Case:
    • If the current node is
      None
      , return 0 because there is no number formed.
  4. Leaf Node Check:
    • If the current node is a leaf (both left and right children are
      None
      ), then the sum formed up to this node is a complete number. Return this sum.
  5. Recursive Case:
    • Recursively call the
      dfs
      function for the left and right children while passing the updated sum.
    • Sum the results from the left and right recursive calls.
  6. Returning the Final Sum:
    • Initialize the DFS with the root node and the initial sum as 0 and return the result.
    ### Step-by-Step Explanation / Detailed Steps in Pseudocode ###
  7. Define node structure:
    • Each node in the tree will have a value (
      val
      ), a left child (
      left
      ), and a right child (
      right
      ).
  8. Define the main solution class:
    • Inside this class, define a method
      sumNumbers
      .
  9. Define the helper DFS function:
    • This function will take a node and the current value formed from the root to that node.
    • If the node is
      None
      , return 0 (base case).
    • Update the current value by multiplying it by 10 and adding the node's value.
    • If the node is a leaf (both children are
      None
      ), return the current value (leaf node case).
    • Sum the results from the left and right children.
  10. Call the DFS function from the root:
    • Initialize the DFS by calling it with the root node and 0.
    • Return the result of the DFS call.
### Pseudocode with Comments ###
                                            
// Define a node structure to represent each node in the binary tree
class TreeNode:
    function __init__(val=0, left=null, right=null):
        self.val = val   // value of the node
        self.left = left // left child
        self.right = right // right child

                                        
                                            
// Define the main solution class containing the method to calculate sum
class Solution:
    function sumNumbers(root):
        // Define the helper function for DFS traversal
        function dfs(node, current_sum):
            if node is null:
                return 0 // base case: if the node is null, no number is formed

            // Update the current sum to include this node's value
            current_sum = current_sum * 10 + node.val

            // Check if the node is a leaf node (no children)
            if node.left is null and node.right is null:
                return current_sum // return the complete number for this path

            // Recursively calculate the sum for the left and right children
            left_sum = dfs(node.left, current_sum)
            right_sum = dfs(node.right, current_sum)

            // Return the total sum from both subtrees
            return left_sum + right_sum
        
        // Initialize the DFS with the root node and initial sum of 0
        return dfs(root, 0)

                                        
In this pseudocode:
  • TreeNode class serves as the blueprint for constructing each node in the binary tree.
  • Solution class contains the
    sumNumbers
    method, which in turn uses a helper method
    dfs
    to perform the DFS traversal and calculate the sum of all root-to-leaf numbers.
  • The
    dfs
    function is responsible for traversing the tree, updating current path sums, and checking for leaf nodes to aggregate the results.