C# Solution


  • 0
    public class Solution {
        public int DepthSum(IList<NestedInteger> nestedList) {
            int sum = 0;
            
            foreach (var item in nestedList)
                sum += GetSum(item, 1);
            
            return sum;
        }
        
        private int GetSum(NestedInteger nestedList, int weight)
        {
            int sum = 0;
            
            if (nestedList.IsInteger())
                sum += nestedList.GetInteger() * weight;
            else
                foreach (var item in nestedList.GetList())
                    sum += GetSum(item, weight + 1);
            
            return sum;
        }
    }

Log in to reply
 

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