/* * 364. Nested List Weight Sum II * 2016-7-15 by Mingyang * 这个是I的升级,I中是count+1,我这里就应该count-1 * 那么就应该最开始的时候把最深的depth找到 */ public int depthSumInverse(List<NestedInteger> nestedList) { int count = findDepth(nestedList, 1); return depthSumHelper1(nestedList, count); } public int findDepth(List<NestedInteger> nestedList, int curDepth) { int res = 0; for(NestedInteger list: nestedList) { if(!list.isInteger()) { res = Math.max(res, findDepth(list.getList(), curDepth+1)); } } return Math.max(res, curDepth); } public int depthSumHelper1(List<NestedInteger> nestedList, int count) { int res = 0; for(NestedInteger list: nestedList) { if(list.isInteger()) res += list.getInteger()*count; if(!list.isInteger()) res += depthSumHelper1(list.getList(), count-1); } return res; }