题目:
代码:
class Solution {
public:
void dfs(TreeNode * root,int curval,int &ans){
curval=curval*10+root->val;
if(root->left==NULL&&root->right==NULL){
ans+=curval;
return;
}
if(root->left!=NULL) dfs(root->left,curval,ans);
if(root->right!=NULL) dfs(root->right,curval,ans);
}
int sumNumbers(TreeNode* root) {
if(root==NULL) return 0;
int ans=0;
dfs(root,0,ans);
return ans;
}
};