Simple understand solution


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

Log in to reply
 

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