# Binary Tree Level Sum

{% embed url="<https://www.lintcode.com/problem/482/>" %}

## Solution 1 (Java)

```java
/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param root: the root of the binary tree
     * @param level: the depth of the target level
     * @return: An integer
     */
    public int levelSum(TreeNode root, int level) {
        int sum = 0;
        if (root == null || level == 0) return sum;
        return bfs(root, sum, level);
    }

    private int bfs(TreeNode root, int sum, int level) {
        List<Integer> result = new ArrayList<>();
        result.add(root.val);
        Queue<TreeNode> q = new ArrayDeque<>();
        q.offer(root);
        while (level > 1 && !q.isEmpty()) {
            int size = q.size();
            int levelSum = 0;
            for (int i = 0; i < size; i++) {
                TreeNode curr = q.poll();
                if (curr.left != null) {
                    levelSum += curr.left.val;
                    q.offer(curr.left);
                }
                if (curr.right != null) {
                    levelSum += curr.right.val;
                    q.offer(curr.right);
                }
            }
            result.add(levelSum);
            level--;
        }
        return result.get(result.size() - 1);
    }
}
```

### Notes

* BFS

## Solution 2 (Java)

```java
/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param root: the root of the binary tree
     * @param level: the depth of the target level
     * @return: An integer
     */
    public int levelSum(TreeNode root, int level) {
        int sum = 0;
        if (root == null || level == 0) return sum;
        return bfs(root, sum, level);
    }

    private int bfs(TreeNode root, int sum, int level) {
        Queue<TreeNode> q = new ArrayDeque<>();
        q.offer(root);
        while (!q.isEmpty()) {
            int size = q.size();
            int levelSum = 0;
            level--;
            for (int i = 0; i < size; i++) {
                TreeNode curr = q.poll();
                if (level == 0)
                    levelSum += curr.val;
                if (curr.left != null)
                    q.offer(curr.left);
                if (curr.right != null)
                    q.offer(curr.right);
            }
            if (level == 0) return levelSum;
        }
        return sum;
    }
}
```

### Notes

* Optimized BFS


---

# 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://blog.yushunchen.com/algo/binary-tree/2.-classical-questions/binary-tree-level-sum.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.
