Ninja technique🥷 to ACE DSA Interviews.
isLeft
to indicate whether the current node is left child or not.isLeft = true
.class Solution {
private:
int sum = 0;
public:
int sumOfLeftLeaves(TreeNode* root, bool isLeft = false)
{
if (root == NULL) return 0;
if (isLeft && root->left == NULL && root->right == NULL) sum += root->val;
sumOfLeftLeaves(root->left, true);
sumOfLeftLeaves(root->right, false);
return sum;
}
};