LC: 339. Nested List Weight Sum - spiralgo/algorithms GitHub Wiki
The Essence:
In computer science, mathematics and almost every single scientific field, many problems and concepts can be reduced to other problems and concepts by a change in perspective:
- The nested list data structure is almost equivalent to a
directed N-ary tree. - Each (nested) list is basically a child of the current list being processed, and it has its own children.
- The task is then to sum all the values of the leaves multiplied by their respective depth.
Details:
The traversal of the list can be done using DFS, which makes keeping track of the depth easy. It can also be done using BFS but it's slightly more complicated to keep track of the depth.