AlgoMaster Logo

Binary Tree Paths

Ashish

Ashish Pratap Singh

easy

Problem Description

Solve it on LeetCode

Approaches

1. Recursive DFS Approach

The recursive DFS approach is straightforward and follows the natural structure of a binary tree. The idea is to start from the root node and explore each path from the root to the leaf nodes. Whenever a leaf node is reached, the path from the root to this leaf node is a completed path and can be stored.

Steps:

  • Traverse the tree recursively. Start from the root and keep track of the path.
  • If a leaf node is reached (both left and right children are null), the current path is saved.
  • Recursively explore both left and right subtrees while updating the current path until all paths are explored.

Code:

2. Iterative DFS Approach

In this approach, we simulate the behavior of a recursive DFS using an explicit stack. This is useful when recursion depth might be a concern.

Steps:

  • Use a stack to manage nodes to be visited along with the path to each node.
  • Similar to the recursive approach, but maintain state explicitly using stack operations.
  • When a node is a leaf, record its path.

Code:

3. BFS Approach Using a Queue

This approach employs breadth-first traversal using a queue to explore the tree level by level. It applies a similar logic as DFS but tracks paths in parallel for nodes on the same level.

Intuition:

  • Traverse using a queue instead of a stack, maintaining the node and its path.
  • As each node is processed, add its children (with updated paths) to the traversal queue.
  • Whenever a leaf node is encountered, cache its path.

Code: