Question
Find the sum of all left leaves in a given binary tree.
Difficulty:Easy
Category:Tree
Analyze
Solution
// Runtimes: 4ms
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if (!root) return 0;
if (root->left && !root->left->left && !root->left->right) return root->left->val + sumOfLeftLeaves(root->right);
int l = sumOfLeftLeaves(root->left);
int r = sumOfLeftLeaves(root->right);
return l + r;
}
};