L4. Binary Tree Traversals in Binary Tree | BFS | DFS
Navigating, inspecting, or solving problems on trees fundamentally requires visiting every single node in a specific, systematic order. This chapter explores the two primary tree traversal strategies: Depth First Search (DFS)—further broken down into Inorder, Preorder, and Postorder traversals—and Breadth First Search (BFS), which operates level by level.
1. Why Do We Need Tree Traversals?
In linear data structures like arrays, linked lists, or stacks, elements are stored sequentially, making traversal straightforward (from index to ). However, hierarchical structures like trees scatter nodes across multiple branches and levels.
- The Problem: You cannot simply use a single loop to visit every node because there is no single linear path.
- The Requirement: To search for an element, print values, calculate heights, or serialize a tree, you need a deterministic strategy to visit every node exactly once without getting lost in branches.
- The Solution: Tree traversal algorithms provide exact roadmaps, categorized into two broad families:
- Depth First Search (DFS): Dive deep down the subtrees first before exploring neighbors.
- Breadth First Search (BFS): Explore all nodes at the present depth (level by level) before moving to the next level.
2. Core Mental Model: DFS vs. BFS
To intuitively grasp the difference between the two traversal families, consider how you would explore a multi-room building or a corporate hierarchy:
- DFS (Depth First Search): Imagine entering a large building, walking down the leftmost corridor, entering the first room on the left, walking into its innermost closet, exploring everything down that specific vertical path to the very bottom, and only backing up when you hit a dead end.
- BFS (Breadth First Search): Imagine standing on the ground floor and inspecting every room on the ground floor from left to right before walking up the stairs to inspect every room on the first floor, and so on, level by level.
3. Depth First Search (DFS) Fundamentals
Depth First Search explores a tree by going as deep as possible along each branch before backtracking. Depending on when you process the current node (the root) relative to its left and right subtrees, DFS splits into three distinct variants:
Note: Whenever you apply these formulas to a subtree, treat that subtree as an independent mini-tree with its own root, left child, and right child. Recursion naturally handles this propagation.
4. Detailed Walkthrough 1: Standard Tree DFS
Consider a sample binary tree structured as follows:
- Root:
1 - Left Subtree of 1: Root
2, with left child4and right child5 - Right Subtree of 1: Root
3, with left child6and right child7
1
/ \
2 3
/ \ / \
4 5 6 7
A. Inorder Traversal (Left Root Right)
- Go to the extreme left subtree (
Node 2's left subtree, which is4). - Apply Inorder on subtree rooted at
2:- Left:
4 - Root:
2 - Right:
5 - Result for left side:
4, 2, 5
- Left:
- Process the main root:
1 - Go to the right subtree (
Node 3), applying Left Root Right:- Left:
6 - Root:
3 - Right:
7 - Result for right side:
6, 3, 7
- Left:
- Final Inorder Traversal Output:
4, 2, 5, 1, 6, 3, 7
B. Preorder Traversal (Root Left Right)
- Process the main root first:
1 - Go to the left subtree (
Node 2), applying Root Left Right:- Root:
2 - Left:
4 - Right:
5 - Result for left side:
2, 4, 5
- Root:
- Go to the right subtree (
Node 3), applying Root Left Right:- Root:
3 - Left:
6 - Right:
7 - Result for right side:
3, 6, 7
- Root:
- Final Preorder Traversal Output:
1, 2, 4, 5, 3, 6, 7
C. Postorder Traversal (Left Right Root)
- Go to the left subtree (
Node 2), applying Left Right Root:- Left:
4 - Right:
5 - Root:
2 - Result for left side:
4, 5, 2
- Left:
- Go to the right subtree (
Node 3), applying Left Right Root:- Left:
6 - Right:
7 - Root:
3 - Result for right side:
6, 7, 3
- Left:
- Process the main root last:
1
- Final Postorder Traversal Output:
4, 5, 2, 6, 7, 3, 1
5. Detailed Walkthrough 2: Modified Larger Tree DFS
Let's expand our tree to test our recursive mental model on a slightly more complex structure:
1
/ \
2 3
/ \ / \
4 5 6 7
/ / \
8 9 10
A. Inorder Traversal (Left Root Right)
- Left Subtree (Root 2):
- Its left child is
4, which has a left child8. - Traversing
4's subtree (Left Root Right):8, 4. - Then root
2, then right5. - Subtree result:
8, 4, 2, 5.
- Its left child is
- Main Root:
1. - Right Subtree (Root 3):
- Left is
6. - Root is
3. - Right is subtree
7with children9(left) and10(right). Applying Inorder on7's subtree gives9, 7, 10. - Subtree result:
6, 3, 9, 7, 10.
- Left is
- Final Inorder Traversal Output:
8, 4, 2, 5, 1, 6, 3, 9, 7, 10
B. Preorder Traversal (Root Left Right)
- Main Root:
1. - Left Subtree (Root 2):
- Root:
2 - Left branch:
4(which has left child8) Root4, Left8. - Right branch:
5. - Subtree result:
2, 4, 8, 5.
- Root:
- Right Subtree (Root 3):
- Root:
3 - Left:
6 - Right branch (Root
7with children9and10) Root7, Left9, Right10. - Subtree result:
3, 6, 7, 9, 10.
- Root:
- Final Preorder Traversal Output:
1, 2, 4, 8, 5, 3, 6, 7, 9, 10
C. Postorder Traversal (Left Right Root)
- Left Subtree (Root 2):
- Leftmost path: node
8is processed, then node4's right is empty, then root4. So8, 4. - Right child of
2:5. - Root of subtree:
2. - Subtree result:
8, 4, 5, 2.
- Leftmost path: node
- Right Subtree (Root 3):
- Left child:
6. - Right subtree rooted at
7(left9, right10, root7)9, 10, 7. - Root of subtree:
3. - Subtree result:
6, 9, 10, 7, 3.
- Left child:
- Main Root:
1.
- Final Postorder Traversal Output:
8, 4, 5, 2, 6, 9, 10, 7, 1
6. Breadth First Search (BFS) Traversal
While DFS dives deep into branches using recursion or stacks, Breadth First Search (BFS) inspects trees horizontally, level by level.
- Level-Order Nature: BFS processes all nodes at depth (the root), then all nodes at depth (children of root), then depth (grandchildren), and so on.
- Simplicity: Conceptually, BFS is often considered more intuitive because it mirrors natural layer-by-layer scanning.
Using our standard tree:
1
/ \
2 3
/ \ / \
4 5 6 7
- Level 0:
1 - Level 1:
2, 3 - Level 2:
4, 5, 6, 7 - Final BFS Traversal Output:
1, 2, 3, 4, 5, 6, 7
7. Practical Study Guide & Mastery Technique
If tree traversals feel confusing or abstract at first glance, do not worry—this is a universal hurdle in Data Structures & Algorithms.
Expert Recommendation: Do not rely solely on mental visualization. Take a blank sheet of paper or a notebook, draw 10 random binary trees of varying shapes and depths, and manually write out their Inorder, Preorder, Postorder, and BFS traversals. Doing this consistently for a few days builds unbreakable intuition.
Summary and Key Takeaways
- Tree Traversals: Essential techniques required to visit every node in a tree systematically to solve algorithmic problems.
- Depth First Search (DFS): Explores deep down subtrees before backtracking, dividing into three variants based on root placement.
- Inorder Traversal: Follows the
Left -> Root -> Rightsequence. - Preorder Traversal: Follows the
Root -> Left -> Rightsequence (root is visited before subtrees). - Postorder Traversal: Follows the
Left -> Right -> Rootsequence (root is visited after subtrees). - Mnemonic Rule: "Pre" = Root first; "In" = Root in middle; "Post" = Root last.
- Breadth First Search (BFS): Explores trees horizontally, visiting nodes level by level from top to bottom.
- Practice Routine: Drawing custom trees on paper and manually working through recursive subtree expansions is the fastest path to algorithmic mastery.