AC java recursion code


  • 0
    U
    public class Solution {
    public int depthSum(List<NestedInteger> nestedList) {
         return helper(nestedList, 1);
        
    }
    private int helper(List<NestedInteger> nestedList, int weight){
        int res=0;
        
        for(int i=0;i<nestedList.size();i++){
            if(nestedList.get(i).isInteger()){
                int temp= (int)nestedList.get(i).getInteger()*weight;
        
                res+= temp;
                  
            }
            else{
                res+=helper( nestedList.get(i).getList(),  weight+1);
                
                
            }
        }
        return res;
    }
    

    }


Log in to reply
 

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