Inorder, Preorder and Postorder Traversal in BFS & DFS

1 min 30 sec read Basic


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

 

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python