Tree Traversal
Tree traversal is the process of visiting each node in a tree data structure exactly once in a systematic way.
Depth First Search
144. Binary Tree Preorder Traversal
Preorder traversal visits nodes in the order: Root → Left → Right. This means we process the current node first, then recursively traverse the left subtree, followed by the right subtree.
- Time
- O(n)
dfsvisits each node exactly once.- Space
- O(h)
- The recursion call stack grows with tree height
h.
94. Binary Tree Inorder Traversal
Inorder traversal visits nodes in the order: Left → Root → Right. For binary search trees, this produces values in ascending sorted order.
- Time
- O(n)
dfsvisits each of thennodes exactly once.- Space
- O(h)
- The recursion call stack grows with tree height
h(worst caseO(n)for a skewed tree,O(log n)for a balanced one).
145. Binary Tree Postorder Traversal
Postorder traversal visits nodes in the order: Left → Right → Root. This is useful when you need to process children before their parent (e.g., deleting nodes, calculating directory sizes).
- Time
- O(n)
dfsvisits each of the tree'snnodes exactly once.- Space
- O(h)
- The recursion call stack grows with the tree's height
h.
606. Construct String from Binary Tree
Serialize the tree in preorder with parentheses around each child subtree. The only subtlety: a node with a right child but no left child must still emit an empty () for the missing left, so the structure is unambiguous. Empty parentheses are otherwise omitted.
- Time
- O(n)
recursionvisits each of thennodes exactly once, contributing its value and parentheses to the result list.- Space
- O(h)
- The recursion stack goes one frame deep per level, up to the tree height
h.
Breadth First Search
102. Binary Tree Level Order Traversal
Level order traversal visits nodes level by level from top to bottom, left to right. This is also known as Breadth-First Search (BFS) for trees.
- Time
- O(n)
- The
while queueloop dequeues every node exactly once viaqueue.popleft(), so the total work across all levels is a singleO(n)pass, wherenis the number of nodes. - Space
- O(w)
queueholds at most one full level's worth of nodes at a time - in the worst case (the widest level of the tree) that iswnodes, wherewis the tree's max width.
107. Binary Tree Level Order Traversal II
- Time
- O(n)
- Every node is dequeued and processed exactly once across all levels -
nnodes total. - Space
- O(w)
queueholds at most one full level at a time, so it grows to the tree's max widthw.
103. Binary Tree Zigzag Level Order Traversal
- Time
- O(n)
- Each of the
nnodes is popped fromqueueand appended tolevelexactly once, with O(1) deque operations for the zigzag insert. - Space
- O(w)
queueholds at most one full level, bounded by the tree's max widthw.
Vertical Order Traversal
314. Binary Tree Vertical Order Traversal
Vertical order traversal groups nodes by their horizontal distance from the root. Root is at column 0, left children decrease column by 1, right children increase column by 1. Within each column, nodes appear from top to bottom.
- Time
- O(n)
nis the number of nodes - the BFS visits every node exactly once, and level-order already delivers each column top-to-bottom, so no separate sort is needed.- Space
- O(n)
columnsstores every node's value, up tonentries total.queueholds at most one full level's worth of nodes, which does not exceedn.
987. Vertical Order Traversal of a Binary Tree
- Time
- O(n + n log n)
- The BFS visits every node once -
O(n)- thensorted(columns[col])runs per column; summed across all columns the elements sorted totaln, so the worst case (all nodes sharing one column) costsO(n log n). - Space
- O(sort + n)
columnsstores every(row, val)pair,O(n);queueadds at mostO(w), which never exceedsO(n).- Sorting algorithms are typically
O(log n)space (in-place, recursion stack only), but Python'ssorted()is Timsort, which allocates up toO(n)auxiliary space in the worst case - that's whatsortstands for here.