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;
  }
};
By guozetang            Updated: 2020-09-19 13:02:30

results matching ""

    No results matching ""