/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @return int整型 */ int f(TreeNode* root,int count) { int sum=0; if(root->left==NULL&&root->right==NULL) { sum=count; } if(root->left!=NULL) { count=(count*10)+root->left->val; sum+=f(root->left,count); } if(root->right!=NULL) { if(count==1&&root->val==0) { count=0; } if(count>9&&root->left!=NULL) { count/=10; } count=(count*10)+root->right->val; sum+=f(root->right,count); } return sum; } int sumNumbers(TreeNode* root) { int count=root->val; if(root==NULL) { return 0; } return f(root,count); } };