leetcode:129. 求根到叶子节点数字之和(深搜)s
程序员文章站
2023-12-23 17:15:57
...
题目:
代码:
int all=0;
void f(TreeNode* r,int c)
{
if(r==NULL)
{
all+=c;
return;
}
c=c*10+r->val;
if(r->right==NULL&&r->left==NULL)
{
all+=c;
return;
}
if(r->right==NULL&&r->left!=NULL){
f(r->left,c);
}
if(r->right!=NULL&&r->left==NULL){
f(r->right,c);
}
if(r->right!=NULL&&r->left!=NULL){
f(r->left,c);
f(r->right,c);
}
}
int main()
{
TreeNode* root;
f(root,0);
return all;
}