Preorder traversal is a way of exploring a tree where we visit each node before its children. The traversal follows a root-left-right sequence: we process the current node, then recursively traverse the left subtree, and finally the right subtree.
root of the tree.Preorder traversal is the right fit for cloning a tree, serializing a tree structure, or evaluating prefix expressions. Its time complexity is O(n) since we visit each node once. Space complexity is O(h) in the worst case, where his the tree's height, because of the recursive call stack.
Inorder traversal is a way of exploring a tree where we visit each node between its left and right children. The traversal follows a left-root-right sequence: first we recursively traverse the left subtree, then process the current node, then traverse the right subtree.
root of the tree, processed after its left subtree.Inorder traversal is especially useful for binary search trees, since it visits nodes in ascending order. It's also how you produce infix expressions from expression trees. Like preorder, its time complexity is O(n) and its space complexity is O(h) in the worst case, where his the tree's height, because of the recursive call stack.
Postorder traversal is a way of exploring a tree where we visit each node after its children. The traversal follows a left-right-root sequence: first we recursively traverse the left subtree, then the right subtree, then process the current node.
root of the tree is the last node processed.Postorder traversal is the right tool for operations that need to process child nodes before their parents, like deleting a tree or evaluating postfix expressions. Like the other traversal methods, its time complexity is O(n) and its space complexity is O(h) in the worst case, where his the tree's height, because of the recursive call stack.
Level order traversal, also known as breadth-first search (BFS) for trees, is a way of exploring a tree where we visit nodes level by level, from left to right. Unlike the previous traversals, level order is usually implemented iteratively with a queue rather than recursively.
root node. The queue keeps track of the nodes we still need to visit at each level.dequeue a node, process it (print its value or add it to the result list), and enqueue its children (left first, then right) if they exist.Level order traversal is useful for tasks that process nodes based on their distance from the root, like finding the minimum depth of a tree or serializing one. Its time complexity is O(n) since we visit each node once. Space complexity is O(w), where w is the maximum width of the tree, because the queue holds at most all the nodes at the widest level.
The traversal you choose decides what the problem looks like. Inorder gives sorted output on a BST. Preorder lets you serialize. Postorder lets you aggregate from leaves up. Level-order gives you the tree by row. Work through these to internalize when each strategy is the right one:
Completed | Title | Status | Notes | Solution | |
|---|---|---|---|---|---|
Easy | |||||
Easy | |||||
Easy | |||||
Easy | |||||
Medium | |||||
Medium | |||||
Medium | |||||
Medium | |||||
Medium | |||||
Hard | |||||
Hard | |||||
Hard |