Java solution, looks like backtrack


  • 0
    C
    int depth = 1;
    int sum = 0;
    
        public int depthSum(List<NestedInteger> nestedList) {
            for(NestedInteger n : nestedList) {
                if(n.isInteger()) 
                    sum += n.getInteger() * depth;
                else {
                    depth++;
                    depthSum(n.getList());
                    depth--;
                }
            }
            return sum;
        }

Log in to reply
 

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