|
| 1 | +Preorder, inorder, and postorder are three common ways to traverse a binary tree. Each traversal has a specific order in which it visits the nodes in the tree, focusing on when it visits the root (or "current") node relative to its children (left and right). |
| 2 | + |
| 3 | +Here's how each traversal works: |
| 4 | + |
| 5 | +### 1. Preorder Traversal (Root -> Left -> Right) |
| 6 | +- **Process:** Visit the root node first, then recursively visit the left subtree, followed by the right subtree. |
| 7 | +- **Order:** Root -> Left -> Right. |
| 8 | +- **Example:** For a tree structured like this: |
| 9 | + ``` |
| 10 | + 1 |
| 11 | + / \ |
| 12 | + 2 3 |
| 13 | + / \ |
| 14 | + 4 5 |
| 15 | + ``` |
| 16 | + The preorder traversal would visit nodes in this order: **1, 2, 4, 5, 3**. |
| 17 | + |
| 18 | +- **Use Case:** Often used when you want to explore the root or parent nodes before inspecting leaves. It can be useful in scenarios like **copying** a tree or **serializing** it for storage. |
| 19 | + |
| 20 | +### 2. Inorder Traversal (Left -> Root -> Right) |
| 21 | +- **Process:** Visit the left subtree first, then the root node, and finally the right subtree. |
| 22 | +- **Order:** Left -> Root -> Right. |
| 23 | +- **Example:** For the same tree: |
| 24 | + ``` |
| 25 | + 1 |
| 26 | + / \ |
| 27 | + 2 3 |
| 28 | + / \ |
| 29 | + 4 5 |
| 30 | + ``` |
| 31 | + The inorder traversal would visit nodes in this order: **4, 2, 5, 1, 3**. |
| 32 | + |
| 33 | +- **Use Case:** Inorder traversal is especially useful in **binary search trees (BSTs)** because it visits nodes in **ascending order** (if the tree is a BST). It’s also helpful when you want to access nodes in a sorted sequence. |
| 34 | + |
| 35 | +### 3. Postorder Traversal (Left -> Right -> Root) |
| 36 | +- **Process:** Visit the left subtree first, then the right subtree, and finally the root node. |
| 37 | +- **Order:** Left -> Right -> Root. |
| 38 | +- **Example:** For the same tree: |
| 39 | + ``` |
| 40 | + 1 |
| 41 | + / \ |
| 42 | + 2 3 |
| 43 | + / \ |
| 44 | + 4 5 |
| 45 | + ``` |
| 46 | + The postorder traversal would visit nodes in this order: **4, 5, 2, 3, 1**. |
| 47 | + |
| 48 | +- **Use Case:** Postorder traversal is useful when you want to deal with the **child nodes before the parent**. It’s commonly used for tasks like **deleting** or **freeing nodes** in memory and evaluating expression trees. |
| 49 | + |
| 50 | +### Summary Table |
| 51 | + |
| 52 | +| Traversal | Order | Example Order for Above Tree (Root: 1) | Common Uses | |
| 53 | +|--------------|----------------------|-----------------------------------------|--------------------------------------------------| |
| 54 | +| **Preorder** | Root -> Left -> Right | `1, 2, 4, 5, 3` | Serialization, copying trees | |
| 55 | +| **Inorder** | Left -> Root -> Right | `4, 2, 5, 1, 3` | Accessing sorted data in BSTs | |
| 56 | +| **Postorder**| Left -> Right -> Root | `4, 5, 2, 3, 1` | Deleting nodes, evaluating expression trees | |
| 57 | + |
| 58 | +These traversal orders allow us to explore and process trees in different ways, depending on the needs of our algorithm or application. |
| 59 | + |
| 60 | +Certainly! Here’s a list of some popular DFS problems on LeetCode, covering various data structures and applications of DFS such as tree traversal, graph traversal, and backtracking: |
| 61 | + |
| 62 | +### Binary Tree DFS Problems |
| 63 | +1. **[144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)** |
| 64 | +2. **[94. Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)** |
| 65 | +3. **[145. Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)** |
| 66 | +4. **[104. Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)** |
| 67 | +5. **[112. Path Sum](https://leetcode.com/problems/path-sum/)** |
| 68 | +6. **[113. Path Sum II](https://leetcode.com/problems/path-sum-ii/)** |
| 69 | +7. **[124. Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)** |
| 70 | +8. **[236. Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)** |
| 71 | + |
| 72 | +### Graph DFS Problems |
| 73 | +1. **[200. Number of Islands](https://leetcode.com/problems/number-of-islands/)** |
| 74 | +2. **[130. Surrounded Regions](https://leetcode.com/problems/surrounded-regions/)** |
| 75 | +3. **[797. All Paths From Source to Target](https://leetcode.com/problems/all-paths-from-source-to-target/)** |
| 76 | +4. **[733. Flood Fill](https://leetcode.com/problems/flood-fill/)** |
| 77 | +5. **[695. Max Area of Island](https://leetcode.com/problems/max-area-of-island/)** |
| 78 | +6. **[417. Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/)** |
| 79 | +7. **[547. Number of Provinces](https://leetcode.com/problems/number-of-provinces/)** |
| 80 | +8. **[785. Is Graph Bipartite?](https://leetcode.com/problems/is-graph-bipartite/)** |
| 81 | + |
| 82 | +### Backtracking DFS Problems |
| 83 | +1. **[46. Permutations](https://leetcode.com/problems/permutations/)** |
| 84 | +2. **[78. Subsets](https://leetcode.com/problems/subsets/)** |
| 85 | +3. **[39. Combination Sum](https://leetcode.com/problems/combination-sum/)** |
| 86 | +4. **[77. Combinations](https://leetcode.com/problems/combinations/)** |
| 87 | +5. **[51. N-Queens](https://leetcode.com/problems/n-queens/)** |
| 88 | +6. **[52. N-Queens II](https://leetcode.com/problems/n-queens-ii/)** |
| 89 | +7. **[212. Word Search II](https://leetcode.com/problems/word-search-ii/)** |
| 90 | +8. **[22. Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)** |
| 91 | + |
| 92 | +### Advanced DFS and Path-Finding Problems |
| 93 | +1. **[329. Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/)** |
| 94 | +2. **[1245. Tree Diameter](https://leetcode.com/problems/tree-diameter/)** |
| 95 | +3. **[399. Evaluate Division](https://leetcode.com/problems/evaluate-division/)** |
| 96 | +4. **[684. Redundant Connection](https://leetcode.com/problems/redundant-connection/)** |
| 97 | +5. **[1376. Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees/)** |
| 98 | + |
| 99 | +These problems showcase DFS’s versatility, from simple traversals and connectivity checks to more complex recursive backtracking challenges. Let me know if you’d like more information on any of these! |
0 commit comments