/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumNumbers(TreeNode *root) {
if(root == NULL){
return 0;
}
long long ret = 0;
queue<TreeNode*> tnq;
tnq.push(root);
while(!tnq.empty()){
TreeNode* tmp = tnq.front();
tnq.pop();
bool isleaf = true;
if(tmp->left){
isleaf = false;
tmp->left->val = tmp->val * 10 + tmp->left->val;
tnq.push(tmp->left);
}
if(tmp->right){
isleaf = false;
tmp->right->val = tmp->val * 10 + tmp->right->val;
tnq.push(tmp->right);
}
if(isleaf){
ret += tmp->val;
}
}
return ret;
}
};