# My easy to understand Java solution

• ``````public class Solution {
int sum = 0;
public int depthSumInverse(List<NestedInteger> nestedList) {
//find the max depth first
int depth = maxDepth(nestedList, 1);
//compute sum over max depth
helper(nestedList, depth);
return sum;
}

private int maxDepth(List<NestedInteger> list, int depth) {
if(list.size() == 0) {
return 1;
}
//Maintain the current maximum for the given level/list
int maxDepthAtCurrLevel = depth;
for(int i = 0; i < list.size(); i++) {
NestedInteger n = list.get(i);
if(!n.isInteger()) {
int d = 1 + maxDepth(n.getList(), depth);
maxDepthAtCurrLevel = Math.max(d, maxDepthAtCurrLevel);
}
}
return maxDepthAtCurrLevel;
}

private void helper(List<NestedInteger> list, int currDepth) {
if(currDepth < 1 || list.size() == 0) {
return;
}
for(int i = 0; i < list.size(); i++) {
NestedInteger n = list.get(i);
if(n.isInteger()) {
int x = n.getInteger();
sum +=  x * currDepth;
}
else {
helper(n.getList(),currDepth-1);
}
}
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.