Iterative C++ solution using postorder traversal to treat the nodes

```
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumNumbers(TreeNode* root) {
stack<TreeNode*> nodes;
if (!root)
return 0;
int total = 0;
int current = 0;
TreeNode* last = nullptr;
while (root || !nodes.empty())
{
if (root)
{
nodes.push(root);
current *= 10;
current += root->val;
root = root->left;
}
else
{
root = nodes.top();
if (root->right && root->right != last)
{
root = root->right;
}
else
{
nodes.pop();
last = root;
// only add sum of leaf node
if (root->right == nullptr && root->left == nullptr)
total += current;
current /= 10;
root = nullptr;
}
}
}
return total;
}
};
```