/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型
*/
int sum = 0;
void dfs(TreeNode* root,string num){
if(root == NULL)
return;
num += to_string(root->val);
if(root->left == NULL && root->right == NULL){
sum += atoi(num.c_str());
return;
}
// if(root->right == NULL){
// sum += atoi(num.c_str());
// dfs(root->left,num);
// }
// else if(root->left == NULL){
// sum += atoi(num.c_str());
// dfs(root->right,num);
// }
else{
dfs(root->left,num);
dfs(root->right,num);
}
}
int sumNumbers(TreeNode* root) {
// write code here
if(root == NULL)
return 0;
dfs(root,"");
return sum;
}
};