BFS and DFS in Data Structure | Inorder, Preorder and Postorder Traversal with practical example.

Article by: Manish Methani

Last Updated: October 9, 2021 at 10:04am IST
1 min 30 sec read

Depth first Search (DFS) is used to traverse a tree in any of following three ways listed below :

1) Inorder Traversal (Left - Root - Right)
2) Preorder Traversal (Root - Left- Right)
3) Postorder Traversal (Left - Right - Root)

Consider the following example for better visual illustration



DFS of given tree,



1) Inorder Traversal :-  4 2 5 1 3 6
2) Preorder Traversal :- 1 2 4 5 3 6
3) Postorder Traversal :- 4 5 2 6 3 1

While traversing a tree you should divide parts into Left, Root, and Right which makes work easier while traversing. If tree contains more levels again divide them into Left-Root-Right. Then you can apply the DFS traversal with any of given three golden rules.

Remember Golden Rules of DFS Traversal:-

1) Inorder traversal (Left - Root - Right)
2) Preorder traversal (Root - Left- Right)
3) Postorder traversal (Left - Right - Root)



2) Breadth First Traversal(BFS)



Breadth First Traversal is also called Level Order Traversal in which you have to traverse level by level from root node.

BFS of given tree is 1 2 3 4 5 6


Simplified Coding for Beginners.

Download the Codzify
Mobile App

Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.


The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test


This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>