# 404 Sum of Left Leaves

Find the sum of all left leaves in a given binary tree.

**Example:**

```
    3
   / \
  9  20
    /  \
   15   7

There are two left leaves in the binary tree, with values 
9
 and 
15
 respectively. Return 
24
.
```

**Complexity:** O(N) time and space **The Idea:** Use any kind of traversal and keep track when you are on a left or right branch.

```cpp
int sumOfLeftLeaves(TreeNode* root) {
    int sum = 0;
    preOrderLeftLeafAcc(root, sum, false);
    return sum;
}

void preOrderLeftLeafAcc(TreeNode* root, int &sum, bool isLeft) {
    if (root) {
        if (!root->left && !root->right && isLeft) {
            sum += root->val;
        }

        preOrderLeftLeafAcc(root->left, sum, true);
        preOrderLeftLeafAcc(root->right, sum, false);
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/404-sum-of-left-leaves.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
