LeetCode 404.. 左叶子之和
原题链接
简单
作者:
owonder
,
2020-09-07 15:57:36
,
所有人可见
,
阅读 540
C++ 代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sum=0;
int sumOfLeftLeaves(TreeNode* root) {
if(!root){
return 0;
}
if (isLeaf(root->left))
{
sum+=root->left->val;
}
sumOfLeftLeaves(root->left) ;
sumOfLeftLeaves(root->right);
return sum;
}
bool isLeaf(TreeNode* node){
if (!node) return false;
return !node->left && !node->right;
}
};