N-ary Tree Preorder
LeetCode 589 • Easy • Trees
Input: root = 1 → [3,2,4], 3 → [5,6] → Output: [1,3,5,6,2,4]
Iterative stack preorder: visit node, then push children in reverse so leftmost pops first.
TimeO(n)visit each node
SpaceO(n)stack + output
Stack: []Ans: []
Current
In Stack
Visited
Stack
empty
Ans
[]
Ready
Press Play. Pop, append to ans, push children right-to-left.