C++ concise 4 lines 0ms DFS solution


  • 0
        int DFS(vector<NestedInteger>& N,int depth) {
            int sum=0;
            for(int i=0;i<N.size();i++) sum+=N[i].isInteger() ? N[i].getInteger()*depth : DFS(N[i].getList(),depth+1);
            return sum;
        }
        int depthSum(vector<NestedInteger>& nestedList) {
            return DFS(nestedList,1);
        }
    

Log in to reply
 

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