Sum of Left Leaves

Tags : tree, binary-tree, dfs, bfs, leetcode, cpp, easy

Given the root of a binary tree, return the sum of all left leaves.

Examples #

Example 1:

Input: root = [3,9,20,null,null,15,7]
Output: 24
Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively.

Example 2:

Input: root = [1]
Output: 0

Constraints #

Solutions #

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
  int res = 0;
  void rc(TreeNode* root){
    if(!root or (!root->left and !root->right)) return;
    if(root->left and !root->left->left and !root->left->right) res+= root->left->val;
    rc(root->left);
    rc(root->right);
  }
  int sumOfLeftLeaves(TreeNode* root) {
    rc(root);
    return res;
  }
};