286. Nested List Weight Sum
You are given a nested list of integers — every element is either an integer or another list with the same property, nested arbitrarily deep.
Each integer has a depth: how many lists it sits inside. Elements of the outermost list have depth 1, elements of a list at depth 1 have depth 2, and so on.
Return the weighted sum of the structure: every integer multiplied by its own depth, all added together.
In Python your function receives the structure as a plain list whose elements are ints or further lists. In C++ and Java it receives a list of NestedItem values — each is either an integer leaf (isInt true, integer in value) or a sub-list (isInt false, children in items / list).
Example 1:
Input: nestedList = [[1,1],2,[1,1]]
Output: 10
Explanation: Four 1s sit at depth 2 and one 2 sits at depth 1: 4·(1·2) + 2·1 = 10.
Example 2:
Input: nestedList = [1,[4,[6]]]
Output: 27
Explanation: 1 is at depth 1, 4 at depth 2, and 6 at depth 3: 1·1 + 4·2 + 6·3 = 27.
Constraints:
- The total number of integers and lists is at most 5 · 10⁴.
- -100 ≤ each integer ≤ 100
- The nesting depth is at most 50.
Hints:
The structure is recursive, so let the solution be recursive too: to process a list at depth d, add value · d for each integer element and recurse into each sub-list at depth d + 1.
Prefer an iterative version? Walk the structure level by level with a queue: everything popped in round d is at depth d; push the children of any sub-list for round d + 1.
▶ Run checks these sample cases. Submit also runs hidden edge cases.
Input: nestedList = [[1,1],2,[1,1]]
Expected output: 10