This post is completed by 3 users

  • 0
Add to List
Beginner

94. Depth First Search/Traversal in Binary Tree

Objective: - Given a Binary Search Tree, Do the Depth First Search/Traversal.

Approach:

  1. The approach is quite simple, use Stack.
  2. First, add the add root to the Stack.
  3. Pop out an element from Stack, Print it and add its right and left children to Stack.
  4. Repeat the above two steps until the Stack is empty.

Example:

DFS

Code:

 

Output:

Depth-First-Search :
 1 2 4 5 3 6 7