Very simple c++ solution


  • 0
    K
    class Solution {
    public:
    int depthSum(vector<NestedInteger>& nestedList) {
        int sum=0, level=1;
         helper(nestedList, level, sum);
         return sum;
    }
    void helper(vector<NestedInteger>& nestedList, int level, int & sum){
        for(auto & data : nestedList){
            if(data.isInteger()){
                sum+=level * (data.getInteger());
            }
            else helper(data.getList(), level+1, sum);
        }
        return;
    }
    

    };


Log in to reply
 

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