> For the complete documentation index, see [llms.txt](https://zedive.gitbook.io/project-l/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zedive.gitbook.io/project-l/part-2/graph-search/exhaustive-search/nested-list-weight-sum.md).

# Nested List Weight Sum

## Question ([LC.339](https://leetcode.com/problems/nested-list-weight-sum/))

> Given a nested list of integers, return the weighted sum.

## Example

```
I: [[1,1],2,[1,1]]
O: 2*2 + 2 + 2*2 = 10
I: [1,[4,[6]]]
O: 1 + 4 * 2 + 6 * 3 = 1 + 8 + 18 = 27
```

What about empty list? 0

## DFS Approach

```
1. define subproblem
    dfsSum will sum up all possible sum for the current level
2. recursive calls
    currentSum += sumDfs()
3. stop condition
    end of the list return
```

## Code

```java
public int depthSum2(List<NestedInteger> nestedList) {
    if (nestedList == null || nestedList.size() == 0) {
        return 0;
    }
    int[] sum = new int[1];
    dfsSum(nestedList, sum, 1);
    return sum[0];
}

private void dfsSum(List<NestedInteger> nestedList, 
                    int[] sum, 
                    int level) {
    for (NestedInteger ele : nestedList) {
        if (ele.isInteger()) {
            sum[0] += ele.getInteger() * level;
        } else {
            dfsSum(ele.getList(), sum, level + 1);
        }
    }
}
```

## BFS Approach

What about BFS? It's easy once we see the nested list as tree. When doing the level order traversal, check if it is list, if so put it back in queue.

## Code

```java
public int depthSum(List<NestedInteger> nestedList) {
    if (nestedList == null || nestedList.size() == 0) {
        return 0;
    }
    // init queue
    Queue<NestedInteger> queue = new LinkedList<>();
    for (NestedInteger ele : nestedList) {
        queue.offer(ele);
    }
    int level = 1;
    int sum = 0;
    // BFS
    while (!queue.isEmpty()) {
        int levelSize = queue.size();
        for (int i = 0; i < levelSize; i++) {
            NestedInteger current = queue.poll();
            if (current.isInteger()) {
                sum += current.getInteger() * level;
            } else {
                for (NestedInteger ele : current.getList()) {
                    queue.offer(ele);
                }
            }
        }
        level++;
    }
    // return depth sum
    return sum;
}
```

## Follow Up ([LC.364](https://leetcode.com/problems/nested-list-weight-sum-ii/))

> Now the weight is defined bottom up. The leaf level has weight 1 and so on.

## Example

```
I: [[1,1],2,[1,1]]
O: 8
I: [1,[4,[6]]]
O: 17
```

## DFS Approach

The most obviously approach is doing a two-pass DFS. The first pass is to find the depth. Then the second pass computes the inverse sum with knowledge of the depth.

## BFS Approach

Compound sum approach. Layer up as we go.

```java
public int depthSumInverse(List<NestedInteger> nestedList) {
    if (nestedList == null || nestedList.size() == 0) {
        return 0;
    }
    // init queue
    Queue<NestedInteger> queue = new LinkedList<>();
    for (NestedInteger ele : nestedList) {
        queue.offer(ele);
    }
    int totalSum = 0;
    int pastLevels = 0;
    // BFS
    while (!queue.isEmpty()) {
        int levelSize = queue.size();
        int currentLevel = 0;
        for (int i = 0; i < levelSize; i++) {
            NestedInteger current = queue.poll();
            if (current.isInteger()) {
                currentLevel += current.getInteger();
            } else {
                for (NestedInteger ele : current.getList()) {
                    queue.offer(ele);
                }
            }
        }
        // level++;
        pastLevels += currentLevel;
        totalSum += pastLevels;
    }
    // return depth sum
    return totalSum;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://zedive.gitbook.io/project-l/part-2/graph-search/exhaustive-search/nested-list-weight-sum.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
