/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int sum;
int sum1;
void dfs(struct TreeNode* root)
{
if(root->left==NULL&&root->right==NULL){
sum1=sum1+sum;
}
if(root->left!=NULL){
sum=sum*10+root->left->val;
dfs(root->left);
sum=(sum-root->left->val)/10;
}
if(root->right!=NULL){
sum=sum*10+root->right->val;
dfs(root->right);
sum=(sum-root->right->val)/10;
}
}
int sumNumbers(struct TreeNode* root){
sum=root->val;
sum1=0;
dfs(root);
return sum1;
}
力扣------从根节点到叶节点的路径数字之和
最新推荐文章于 2024-09-02 16:50:54 发布