lc.404 左叶子之和
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int sumOfLeftLeaves(TreeNode* root) {
int n=0;
if(root){
//如果root的左孩子存在,且是叶子节点,则加上它的值
if(root->left&&root->left->left==nullptr&&root->left->right==nullptr)
{n+=root->left->val;}
n+=sumOfLeftLeaves(root->left);//左右递归
n+=sumOfLeftLeaves(root->right);
return n;
}else{//如果root=null,返回0
return 0;
}
}
This post is licensed under CC BY 4.0 by the author.