Just treat the Nested List as a tree, and dfs it:

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

 

posted on 2022-02-08 14:37  阳光明媚的菲越  阅读(16)  评论(0编辑  收藏  举报