Java-DFS-recursive method


  • 0
    B
    public class Solution {
        int result = 0;
        public int depthSum(List<NestedInteger> nestedList) {
            int level = 1;
            for(NestedInteger ni : nestedList){
                helper(ni,level);
            }
            return result;
        }
        private void helper(NestedInteger n, int level){
            if(n.isInteger()){
                result += level * n.getInteger();
            }else{
                List<NestedInteger> nList = n.getList();
                for(NestedInteger ni : nList){
                    helper(ni,level+1);
                }
            }
        }
    

    }


Log in to reply
 

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