1ms Recursive Java Solution


  • 0
    G
    public class Solution {
        public int depthSum(List<NestedInteger> nestedList) {
            return helper(nestedList,1);
        }
        
        private int helper(List<NestedInteger> nestedList, int level){
            int sum = 0;
            for(NestedInteger nestedInt : nestedList){
                if (nestedInt.isInteger()){
                    sum += nestedInt.getInteger()*level;
                }
                else sum += helper(nestedInt.getList(),level+1);
            }
            return sum;
        }
    }
    

Log in to reply
 

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