Advertisement

Sum Root to Leaf Numbers - LeetCode 129 Solution

Sum Root to Leaf Numbers - Complete Solution Guide

Sum Root to Leaf Numbers is LeetCode problem 129, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given the root of a binary tree containing digits from 0 to 9 only. Each root-to-leaf path in the tree represents a number. For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123 . Return the total sum of all root-to-leaf numbers . Test cases are generated so that the answer will fit in a 32-bit integer. A leaf node is a node with no children. Example 1: Input: root = [1,2,3] Output: 25 Explanation: The root-to-leaf path 1->2 represents the number 12 . The root-to-leaf

Detailed Explanation

The problem asks us to calculate the sum of all numbers formed by the paths from the root to each leaf node in a binary tree. Each node in the tree contains a digit from 0 to 9. A path from the root to a leaf represents a number where the digits are concatenated in the order they appear along the path. The goal is to traverse the tree, construct these numbers, and return their sum. For example, if a path is 1 -> 2 -> 3, the number formed is 123. A leaf node is a node with no left or right children. The constraints specify that the tree will have between 1 and 1000 nodes, node values are between 0 and 9, and the depth of the tree will not exceed 10. The answer must fit in a 32-bit integer.

Solution Approach

The solution uses a recursive Depth-First Search (DFS) approach. The `dfs` function takes a node and the 'current number' as input. At each node, it updates the 'current number' by multiplying it by 10 and adding the node's value. If the node is a leaf node, the 'current number' is returned. Otherwise, the function recursively calls itself on the left and right children, passing the updated 'current number'. The results of the recursive calls are summed and returned. The initial call to `dfs` is made with the root node and an initial 'current number' of 0. This traversal effectively explores all root-to-leaf paths and accumulates their numerical values.

Step-by-Step Algorithm

  1. Step 1: Start with the root node and an initial current number of 0.
  2. Step 2: Multiply the current number by 10 and add the value of the current node to it. This forms the number represented by the path from the root to the current node.
  3. Step 3: Check if the current node is a leaf node (no left and no right children).
  4. Step 4: If it is a leaf node, return the current number. This indicates that a complete path from root to leaf has been found.
  5. Step 5: If it is not a leaf node, recursively call the DFS function on the left and right children, passing the updated current number.
  6. Step 6: Sum the results returned by the left and right recursive calls. This sums the numbers formed by all the paths passing through this node.
  7. Step 7: Return the sum from Step 6. This value propagates back up the call stack until it reaches the initial call, which returns the final sum of all root-to-leaf numbers.

Key Insights

  • Insight 1: Depth-First Search (DFS) is a natural fit for traversing the tree and exploring each root-to-leaf path.
  • Insight 2: We can pass the 'current number' being formed down the tree in the recursive calls, avoiding string manipulations and keeping the code efficient.
  • Insight 3: Checking for a leaf node is essential to determine when to add the formed number to the total sum. A leaf node is where a path ends.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(h)

Topics

This problem involves: Tree, Depth-First Search, Binary Tree.

Companies

Asked at: ServiceNow, Visa.