Created
November 8, 2022 23:52
-
-
Save dluciano/dcda752f182d7ef7a28c95237f83ec31 to your computer and use it in GitHub Desktop.
339. Nested List Weight Sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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