# 题目地址：

https://leetcode.com/problems/nested-list-weight-sum-ii/

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
int res = 0;

// sum存前k层的树上前缀和
int sum = 0;
for (NestedInteger num : nestedList) {
queue.offer(num);
}

while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
NestedInteger cur = queue.poll();
if (cur.isInteger()) {
sum += cur.getInteger();
} else {
for (NestedInteger next : cur.getList()) {
queue.offer(next);
}
}
}
// 累加前缀和
res += sum;
}

return res;
}
}

interface NestedInteger {
// Constructor initializes an empty nested list.
// NestedInteger();

// Constructor initializes a single integer.
// NestedInteger(int value);

// @return true if this NestedInteger holds a single integer, rather than a nested list.
boolean isInteger();

// @return the single integer that this NestedInteger holds, if it holds a single integer
// Return null if this NestedInteger holds a nested list
Integer getInteger();

// Set this NestedInteger to hold a single integer.
void setInteger(int value);

// Set this NestedInteger to hold a nested list and adds a nested integer to it.

// @return the nested list that this NestedInteger holds, if it holds a nested list
// Return null if this NestedInteger holds a single integer
List<NestedInteger> getList();
}


06-23 4906

06-24 2571

03-24 87

09-21 87

10-23 36

07-01 253

07-03 702

01-04 475

09-10 14

10-22 97

05-17 336

11-26 13

08-15 12

09-11 20

08-16 4

08-23 388

08-08 488

01-04 270

07-14 409

04-02 9

#### Leetcode 364 Nested List Weight Sum II

©️2019 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客