Given the root of an N-ary tree, return the preorder traversal of its nodes’ values.
Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples).
Input: root = [1,null,3,2,4,null,5,6]
Output: [1,3,5,6,2,4]
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [1,2,3,6,7,4,8,9,10,5,11,12,13,14]
None
.# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
self.val = val
self.children = children if children is not None else []
class Solution:
def preorder(self, root: 'Node') -> List[int]:
def traverse(node):
if node is None:
return
result.append(node.val)
for child in node.children:
traverse(child)
result = []
traverse(root)
return result
class Solution:
def preorder(self, root: 'Node') -> List[int]:
if root is None:
return []
stack, result = [root], []
while stack:
node = stack.pop()
result.append(node.val)
stack.extend(reversed(node.children))
return result
Both techniques ensure that all nodes are visited and processed in preorder fashion. The space complexity is O(n) in the worst case due to the recursion stack or the explicit stack used for traversal.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?