C++ easy solution


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

Log in to reply
 

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