Skip to content

Instantly share code, notes, and snippets.

@dluciano
Created November 8, 2022 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dluciano/dcda752f182d7ef7a28c95237f83ec31 to your computer and use it in GitHub Desktop.
Save dluciano/dcda752f182d7ef7a28c95237f83ec31 to your computer and use it in GitHub Desktop.
339. Nested List Weight Sum
public class Solution {
public int DepthSum(IList<NestedInteger> nestedList) {
int Traverse(in IList<NestedInteger> list, in int depth = 1){
var sum = 0;
foreach(var e in list){
if(e.IsInteger()){
sum += e.GetInteger() * depth;
continue;
}
sum += Traverse(e.GetList(), depth + 1);
}
return sum;
}
return Traverse(nestedList);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment