/**
* 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 sumAll(TreeNode *root,int sum)
{
if(root==NULL) return 0;
else if(root->left==NULL&&root->right==NULL)
return sum*10+root->val;
else
return sumAll(root->left,sum*10+root->val)+sumAll(root->right,sum*10+root->val);
}
int sumNumbers(TreeNode *root) {
return sumAll(root,0);
}
};
* 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 sumAll(TreeNode *root,int sum)
{
if(root==NULL) return 0;
else if(root->left==NULL&&root->right==NULL)
return sum*10+root->val;
else
return sumAll(root->left,sum*10+root->val)+sumAll(root->right,sum*10+root->val);
}
int sumNumbers(TreeNode *root) {
return sumAll(root,0);
}
};