> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/129-sum-root-to-leaf-numbers.md).

# 129 Sum Root to Leaf Numbers

Given a binary tree containing digits from`0-9`only, each root-to-leaf path could represent a number.

An example is the root-to-leaf path`1->2->3`which represents the number`123`.

Find the total sum of all root-to-leaf numbers.

For 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`.

Return the sum = 12 + 13 =`25`.

**The Idea:** Using a single DFS traversal, we can capture the elements as we iterate to build up a new string. Once a leaf is reached, we can push the collected string to a vector. Once we both left and right children are explored, we popback once to obtain an earlier state in the recursion of the string

**Possible Improvements:** We can maintain accumulate the elements immediately rather than storing them into a vector.

```cpp
void dfs_root_sum(TreeNode *cur_node, string &cur_string, vector<int> &root_leaf_paths)
{
    if (cur_node) {
        cur_string += to_string(cur_node->value);

        // is leaf node
        if (!cur_node->left && !cur_node->right) {
            root_leaf_paths.push_back(stoi(cur_string));
        }

        dfs_root_sum(cur_node->left, cur_string, root_leaf_paths);
        dfs_root_sum(cur_node->right, cur_string, root_leaf_paths);
        cur_string.pop_back();
    }
}


int sumNumbers(TreeNode *root)
{
    vector<int> root_leaf_paths;
    string cur_string;

    dfs_root_sum(root, cur_string, root_leaf_paths);

    int total_sum = 0;
    for (int i : root_leaf_paths) {
        total_sum += i;
    }

    return total_sum;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/129-sum-root-to-leaf-numbers.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
