在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
分析:一个简单的题目,只需要判断当前节点是不是左叶子即可
下面看代码
/**
* 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 sumOfLeftLeaves(TreeNode* root) {
int tmp=0;
if(!root)
return 0;
if(root->left!=NULL && root->left->left==NULL && root->left->right==NULL)
tmp=root->left->val;
return sumOfLeftLeaves(root->left)+sumOfLeftLeaves(root->right)+tmp;
}
};