How do you copy a tree? You can't create a child before its parent exists, so the copy has to meet every node before either of its children. Preorder traversal is exactly that reading order: we visit each node before its children, following a root-left-right sequence. We process the current node, then recursively traverse the left subtree, and finally the right subtree. To see each order on real values, this page reuses one example tree throughout: the root 8 has children 3 and 10, the 3 has children 1 and 6, and the 10 has a lone right child 14.
root of the tree.On the example tree, preorder reads 8, 3, 1, 6, 10, 14: the root comes out first, then the entire left branch, then the right. Because parents always precede their children, 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 h is the tree's height, because of the recursive call stack.
What order would print a binary search tree's values from smallest to largest? Inorder traversal is the answer, and it gets there by visiting each node between its left and right children in a left-root-right sequence: first we recursively traverse the left subtree, then process the current node, then traverse the right subtree. Before reading on, predict which node of the example tree (root 8, then 3 and 10, then 1, 6, 14) gets processed first. It isn't the root.
root of the tree, processed after its left subtree.The first node processed is the 1: the walk sinks all the way down the left edge before processing anything. The full order is 1, 3, 6, 8, 10, 14, and that's no coincidence. You might think getting sorted output from a tree requires collecting the values and sorting them. On a binary search tree, inorder traversal hands you ascending order for free, because every node waits behind everything smaller than it. It's also how you produce infix expressions from expression trees. The costs match preorder: O(n) time and O(h) space for the call stack.
How do you delete a tree without ever orphaning a node? You can't remove a parent while its children still point to it, so deletion has to reach the children first. Postorder traversal is built for that: we visit each node after its children, following 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.On the example tree, postorder reads 1, 6, 3, 14, 10, 8: every parent comes out after both of its children, and the root exits last. That makes postorder the right tool for operations that need to process child nodes before their parents, like deleting a tree or evaluating postfix expressions. The costs are the same O(n) time and O(h) space as the other two.
What if you need the tree row by row instead of branch by branch, say, to find the shallowest leaf? 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, because a queue's first-in, first-out order naturally serves nodes in the order their levels were discovered.
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.On the example tree, level order reads 8, then 3, 10, then 1, 6, 14, one row at a time. That makes it 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.
Before you start, quiz yourself on two things from this page: which traversal visits a binary search tree's values in ascending order, and why is postorder the only safe order for deleting a tree? If both answers come back quickly, you're ready. The traversal you choose decides what the problem looks like. Inorder gives sorted output on a BST, preorder lets you serialize, postorder aggregates from the leaves up, and level-order hands 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 |