Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Readme.md

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).

Here's how each traversal works:

1. Preorder Traversal (Root -> Left -> Right)

  • Process: Visit the root node first, then recursively visit the left subtree, followed by the right subtree.

  • Order: Root -> Left -> Right.

  • Example: For a tree structured like this:

          1
         / \
        2   3
       / \
      4   5
    

    The preorder traversal would visit nodes in this order: 1, 2, 4, 5, 3.

  • 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.

2. Inorder Traversal (Left -> Root -> Right)

  • Process: Visit the left subtree first, then the root node, and finally the right subtree.

  • Order: Left -> Root -> Right.

  • Example: For the same tree:

          1
         / \
        2   3
       / \
      4   5
    

    The inorder traversal would visit nodes in this order: 4, 2, 5, 1, 3.

  • 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.

3. Postorder Traversal (Left -> Right -> Root)

  • Process: Visit the left subtree first, then the right subtree, and finally the root node.

  • Order: Left -> Right -> Root.

  • Example: For the same tree:

          1
         / \
        2   3
       / \
      4   5
    

    The postorder traversal would visit nodes in this order: 4, 5, 2, 3, 1.

  • 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.

Summary Table

Traversal Order Example Order for Above Tree (Root: 1) Common Uses
Preorder Root -> Left -> Right 1, 2, 4, 5, 3 Serialization, copying trees
Inorder Left -> Root -> Right 4, 2, 5, 1, 3 Accessing sorted data in BSTs
Postorder Left -> Right -> Root 4, 5, 2, 3, 1 Deleting nodes, evaluating expression trees

These traversal orders allow us to explore and process trees in different ways, depending on the needs of our algorithm or application.

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:

Binary Tree DFS Problems

  1. 144. Binary Tree Preorder Traversal
  2. 94. Binary Tree Inorder Traversal
  3. 145. Binary Tree Postorder Traversal
  4. 104. Maximum Depth of Binary Tree
  5. 112. Path Sum
  6. 113. Path Sum II
  7. 124. Binary Tree Maximum Path Sum
  8. 236. Lowest Common Ancestor of a Binary Tree

Graph DFS Problems

  1. 200. Number of Islands
  2. 130. Surrounded Regions
  3. 797. All Paths From Source to Target
  4. 733. Flood Fill
  5. 695. Max Area of Island
  6. 417. Pacific Atlantic Water Flow
  7. 547. Number of Provinces
  8. 785. Is Graph Bipartite?

Backtracking DFS Problems

  1. 46. Permutations
  2. 78. Subsets
  3. 39. Combination Sum
  4. 77. Combinations
  5. 51. N-Queens
  6. 52. N-Queens II
  7. 212. Word Search II
  8. 22. Generate Parentheses

Advanced DFS and Path-Finding Problems

  1. 329. Longest Increasing Path in a Matrix
  2. 1245. Tree Diameter
  3. 399. Evaluate Division
  4. 684. Redundant Connection
  5. 1376. Time Needed to Inform All Employees

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!