Sum of Left Leaves
code
/**
* 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) {
if(root == NULL)
{
return 0;
}
return helper(root->left, true) + helper(root->right, false);
}
int helper(TreeNode* root, bool bLeft)
{
if(root == NULL)
{
return 0;
}
if(bLeft && root->left == NULL && root->right == NULL)
{
return root->val;
}
int left = helper(root->left, true);
int right = helper(root->right, false);
return left+right;
}
};