class Solution {
public int depthSum(List<NestedInteger> nestedList) {
if (nestedList == null) return 0;
int sum = 0;
int level = 1;
Queue<NestedInteger> queue = new LinkedList<NestedInteger>(nestedList);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
NestedInteger ni = queue.poll();
if (ni.isInteger()) {
sum += ni.getInteger() * level;
}
else {
queue.addAll(ni.getList());
}
}
level++;
}
return sum;
}
}