# 44. 左叶子之和

java

```java
import leetcode.common.TreeNode;
import leetcode.common.TreeUtils;

/**
 * 404. 左叶子之和
 *
 * 计算给定二叉树的所有左叶子之和。
 *
 * 示例：
 *
 *     3
 *    / \
 *   9  20
 *     /  \
 *    15   7
 *
 * 在这个二叉树中，有两个左叶子，分别是 9 和 15，所以返回 24
 *
 */
public class Solution404 {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root==null||(root.left==null&&root.right==null)) return 0;
        return sum(root,1);
    }

    /**
     * 求左叶子节点的和
     * @param root
     * @param left 1：左节点，0：右节点
     * @return
     */
    public int sum(TreeNode root,int left) {
        if(root==null) return 0;
        if(root.left==null&&root.right==null&&left==1) return root.val;
        return sum(root.left,1)+sum(root.right,0);
    }

    public static void main(String[] args){
        TreeNode root=TreeUtils.stringToTreeWith("3 9 # # 20 15 # # 7 # #"," ");
        int r=new Solution404().sumOfLeftLeaves(root);
        System.out.println(r);
    }
}
```


---

# 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://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/shu-ju-jie-gou-lei/44-zuo-ye-zi-zhi-he.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.
