C#C
C#3y ago
morry329#

❔ ✅ comprehension questions for a LeetCode puzzle

I am analysing the following LC solution (the iterative one)
https://leetcode.com/problems/n-ary-tree-preorder-traversal/solutions/3185068/iteratively-and-recursively-method-with-description-c/

I have a questions with regards to a couple of lines in this code.
  1. It's "Stack.Push(root)" instead of "result.Add(root)". I get that the root node shall be added to the return array, but I still do not understand why the stack comes out here.
  2. And the few lines below 1., it's "result.Add(currentNode.val), instead of "Stack.Push(currentNode.val)". Here I am still confused how the stack and the List have been used interchangeably (or non-interchangeably).
  3. The same goes for the line "stack.Push(currentNode.children[i])" , instead of "result.Add(currentNode.val)". Why is the stack push here?
It would be great if anyone could explain to me a bit about those questions.
LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
Was this page helpful?