/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public 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
* public Integer getInteger();
*
* // Set this NestedInteger to hold a single integer.
* public void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void add(NestedInteger ni);
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
public class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
int res = 0;
int tempRes = 0;
Queue<NestedInteger> queue = new LinkedList<NestedInteger>();
for(NestedInteger element : nestedList) {
queue.offer(element);
}
while (!queue.isEmpty()) {
int curSize = queue.size();
int levelSum = 0;
for (int i = 0; i < curSize; i++) {
NestedInteger temp = queue.poll();
if (temp.isInteger()) {
levelSum += temp.getInteger();
} else {
for (NestedInteger sub : temp.getList()) {
queue.offer(sub);
}
}
}
tempRes = tempRes + levelSum;
res += tempRes;
}
return res;
}
}
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
* // Constructor initializes an empty nested list.
* public NestedInteger();
*
* // Constructor initializes a single integer.
* public NestedInteger(int value);
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* public 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
* public Integer getInteger();
*
* // Set this NestedInteger to hold a single integer.
* public void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* public void add(NestedInteger ni);
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* public List<NestedInteger> getList();
* }
*/
public class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
if (nestedList == null) return 0;
Queue<NestedInteger> queue = new LinkedList<NestedInteger>();
int prev = 0;
int total = 0;
queue.addAll(nestedList);
// while (!queue.isEmpty()) {
// int size = queue.size();
// int levelSum = 0;
// for (int i = 0; i < size; i++) {
// NestedInteger current = queue.poll();
// if (current.isInteger()) levelSum += current.getInteger();
// else {
// queue.addAll(current.getList());
// }
// }
// prev += levelSum;
// total += prev;
// }
while (!queue.isEmpty()) {
int size = queue.size();
// int levelSum = 0;
for (int i = 0; i < size; i++) {
NestedInteger current = queue.poll();
if (current.isInteger()) prev += current.getInteger(); // <-- prev is not reset to 0
else {
queue.addAll(current.getList());
}
}
//prev += levelSum;
total += prev;
}
return total;
}
// public int depthSumInverse(List<NestedInteger> nestedList) {
// if (nestedList == null || nestedList.size() == 0) {
// return 0;
// }
// int maxDep = getDep(nestedList, 1);
// return dfsHelper(nestedList, maxDep);
// }
// private int getDep(List<NestedInteger> nl, int level) {
// int dep = 0;
// for (NestedInteger ni : nl) {
// if (!ni.isInteger()) {
// dep = Math.max(dep, getDep(ni.getList(), level + 1));
// }
// }
// return Math.max(level, dep);
// }
// private int dfsHelper(List<NestedInteger> nl, int level) {
// int result = 0;
// for (NestedInteger ni : nl) {
// if (ni.isInteger()) {
// result += ni.getInteger() * level;
// }
// else {
// result += dfsHelper(ni.getList(), level - 1);
// }
// }
// return result;
// }
}