<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p>

<p>Each root-to-leaf path in the tree represents a number.</p>

<ul>
	<li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li>
</ul>

<p>Return <em>the total sum of all root-to-leaf numbers</em>. Test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p>

<p>A <strong>leaf</strong> node is a node with no children.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/num1tree.jpg" style="width: 212px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> 25
<strong>Explanation:</strong>
The root-to-leaf path <code>1-&gt;2</code> represents the number <code>12</code>.
The root-to-leaf path <code>1-&gt;3</code> represents the number <code>13</code>.
Therefore, sum = 12 + 13 = <code>25</code>.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/num2tree.jpg" style="width: 292px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [4,9,0,5,1]
<strong>Output:</strong> 1026
<strong>Explanation:</strong>
The root-to-leaf path <code>4-&gt;9-&gt;5</code> represents the number 495.
The root-to-leaf path <code>4-&gt;9-&gt;1</code> represents the number 491.
The root-to-leaf path <code>4-&gt;0</code> represents the number 40.
Therefore, sum = 495 + 491 + 40 = <code>1026</code>.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 1000]</code>.</li>
	<li><code>0 &lt;= Node.val &lt;= 9</code></li>
	<li>The depth of the tree will not exceed <code>10</code>.</li>
</ul>

class Solution {
public:
    void dfs(TreeNode* root, int n, int& ans) {
        if (!root) return ;
        n = n * 10 + root->val;
        if (!(root->left) && !(root->right)) {
            ans += n;
            cout << ans << '\n';
            n = 0;
        }
        dfs(root->left, n, ans);
        dfs(root->right, n, ans);
    }
    int sumNumbers(TreeNode* root) {
        int ans = 0;
        // recurive dfs
        // dfs(root, 0, ans);

        // iterative dfs
        // stack<pair<TreeNode*, int>> s; // node, cur
        // s.push({root, 0});
        // while (!s.empty()) {
        //     auto [root, cur] = s.top();
        //     s.pop();
        //     cur = cur * 10 + root->val;
        //     if (!(root->left) && !(root->right)) ans += cur;
        //     if (root->right) s.push({root->right, cur});
        //     if (root->left) s.push({root->left, cur});
        // }
        // return ans;

        // Morris traversal preorder case
        TreeNode* cur = root;
        int totS = 0, curS = 0, dep = 0;
        while (cur) {
            if (!(cur->left)) {
                curS = curS * 10 + cur->val;
                if (!(cur->right)) totS += curS;
                cur = cur->right;
            } else {
                TreeNode* prev = cur->left;
                dep = 1;
                while (prev->right && prev->right != cur) {
                    prev = prev->right;
                    dep++;
                }
                if (!(prev->right)) {
                    prev->right = cur;
                    curS = curS * 10 + cur->val;
                    cur = cur->left;
                } else {
                    prev->right = NULL;
                    if (!(prev->left)) totS += curS;
                    curS = curS / pow(10, dep);
                    cur = cur->right;
                }
            }
        }
        return totS;
    }
};
11500