TreesTrees
N-ary Tree Traversal
A rooted tree in which each node can have any number of children, stored as a child list, and traversed with the same DFS/BFS ideas as binary trees.
DFS order
empty
Call stack
empty
1/32DFS (preorder) from A: visit a node, then recurse into each child left to right — the whole first subtree is finished before the second starts.
Being visitedOn stack / in queueDone
PseudocodeLearn N-ary Tree →
1dfs(node): visit(node)2 for child in node.children: dfs(child)3bfs(root): queue = [root]4 while queue: node = queue.popleft(); visit(node)5 for child in node.children: queue.append(child)Variables
modedfs
nodes10
Complexity
access O(n)
search O(n)
insert O(1)
delete O(k)
Speed