TREE PRACTICE PROBLEMS | Tree Traversal Algorithms | DSA Course | GeeksforGeeks

GeeksforGeeks · Beginner ·⚡ Algorithms & Data Structures ·2y ago

Key Takeaways

Implements tree traversal algorithms including InOrder, Postorder, and Preorder Traversals

Full Transcript

hello everyone my name is Karan masu welcome to this video so in the last video we talked about the basics of tree data structure in this video we will be looking at the three standard binary tree traversal algorithms okay so first one is the inorder traversal method another is the pre-order traversal and another one is the post order traversal so what are the differences in all three of this in in order traversal we first Traverse the left sub tree then we note down the root node value and then we Traverse the right sub tree okay in I'll also tell you a trick how to remember this don't worry in pre-order traversal we first Traverse the root node then we Traverse the left sub tree and then we Traverse the right sub tree in the post order traversal we first Traverse the left sub tree then the right sub tree and then the root node okay so let me take an example so let us take an example of a binary tree so let's say this is 10 2 12 14 3 and 9 okay and I'll take the same values here okay so I'll select this and I'll copy it and I'll paste it here okay and I'll also paste it here okay so now what is the in order traversal in order traversal means you first Traverse the left sub tree then root and then the right sub tree okay so you first Traverse the left sub tree so you go to the left now still there is a left subt so you still go to the left so your output is 14 then you Traverse the root node so when you backtrack two is the root node like if I look at the so starting from the root node first we go in the left sub tree because it is first left sub tree now recursively from two also I'll go in the left sub tree so I'll come here so I'll print 14 now there is no more left sub tree so I'll go back now for this sub tree 24 and 3 two is the root node so after left sub tree is done the left sub tree of two is done so root node will be Tak taken and then the right sub tree will be taken so three will be taken then we'll backtrack from here and go back here so 10 left sub tree is over now 10 will be return because it is the root node then we will go into its right sub tree so first left sub tree is done then root is done now we go in the right sub tree so it is 12 and 9 in this order it will be printed right because when we go to 12 here also first left sub tree will be Travers there is no left sube then root will be Travers so 12 will be Travers then then nine will be Travers because it's the right one okay similarly here it will be root left right and here it will be left right root now uh let me tell you a trick how to write down these values on a pen and paper I mean how to do in order traversal pre-order traversal and postorder traversal efficiently on a pen and paper and then I'll also tell you how do you remember this order and then we will look at the code of all three traversals okay so first of all let me just remove this okay and let me paste the same tree here again now see this was the in order traversal right so whenever you want in order traversal what do you need to do is make pointers at the bottom of the nodes okay when you want to do pre-order traversals make it on the left hand side and when you want to do Post order traversal make pointers on the right hand side okay now in all three of them Traverse the peripheral of the tree in this way okay starting from here to here okay you go here and you go here here also okay and here also okay now when you are traversing whenever you encounter this pointer you note down that nodes value so while I'm going here the first pointer the first point which I encounter is this one so I'll note down 14 then from here I'll encounter two so I'll write down two then I'll encounter three so I'll write down three then I'll encounter this point that is 10 so I'll write down 10 then I'll encounter uh here I'll encounter 12 so I'll write down 12 and then I'll encounter nine I'll encounter this point at the last so I'll write down nine you can see we have got the same answer so this is in order traversal now in pre-order traversal it is root left and right so when I start traversing the first of all I'll encounter this point so I'll write down 10 then I'll encounter this point so I'll write down two then I'll encounter this point so I'll write down 14 then I'll encounter this point so I'll write down three then I'll encounter this point so I'll write down 12 and then I'll encounter this point so I'll write down nine okay so I this is the pre-order traversal let's come to the post order traversal so I'll come here come here come here and first I'll encounter this one so I'll write down 14 then I'll encounter this one so I'll write down three then I'll encounter this one so I'll write down two then I'll encounter this one so I'll write down N9 then I'll encounter this one so I'll write down 12 and then I'll encounter this one so I'll write down 10 so this is the post order traversal in with left right and then root is traversed you can also see root is at the end so this root value is at the end okay so this is the basic of in order traversal pre-order traversal and post order traversal now how do you remember this uh this order okay so that you do not make a mistake see this is in order traversal this is pre-order traversal and this is post ordered raval in all three of them left subtree is before the right sub Tree in all three of them left sub tree is before the right sub Tree in in order traversal the root is in order it is in between in the pre-order it it is pre it is before left and right sub Tre in post order traversal it is post so root is after left sub tree and right sub tree so this is left root right this is root left sub tree right sub tree this is left sub tree right sub tree and root now let's look at the actual implementation of all these three questions and submit them so this is the pre-order traversal question let's look at the code we have already understood the concept so we had to complete this function pre-order traversal where root of the binary tree was given and we needed to return the pre-order traversal in a vector so I have taken one vector answer and called the pre-order traversal function and I have passed the root and Vector in that what does pre-order traversal do if the root is not null pre-order means root left sub tree right sub tree so first of all it will push back this nodes value in the answer Vector push back root data then Traverse on the left subre and then Traverse on the right sub so it is root left and right let's submit the code Okay so we have solved this successfully let's look at the next one okay so this question is on post order traversal uh postorder traversal is uh left sub Tre right sub Tre and root node okay so if I look at the code I had to complete this post order function I have taken one vector answer I have taken post order traversal and pass the root node pointer and the answer vector and if root is not null post order means left sub Tre right sub Tre and root node so Traverse on the left sub Traverse on the right sub Tre and finally push back the value of the current root node okay let's submit it okay so we have solved this question successfully now last let us look at the code of in order traversal okay so this is the question on in order traversal let's look at the code for it okay so uh this was the code which I needed to complete uh so I have taken one vector answer and call the in order traversal function pass the root node and the answer vector and here if I look if in order traversal means left sub tree root and right sub tree so if root is not null Traverse on the left sub tree then push back the value of the current rout and then Traverse on the right sub tree okay it will give us the in order traversal let me submit it okay so we have solved this question successfully okay so I hope you have understood all these three traversals completely thank you

Original Description

💻In this video we will be discussing the famous practice questions on Tree Data Structure, ie. InOrder Traversal, Postorder Traversal, and Preorder Traversals. We will understand how these individual traversal algorithms work and also see the code to implement these algorithms. So tag along and master Trees for your next DSA interview. Read More: https://www.geeksforgeeks.org/top-50-tree-coding-problems-for-interviews/ Solve the Problems: 💻 InOrder Traversal: https://www.geeksforgeeks.org/problems/inorder-traversal/1?utm_source=youtube&utm_medium=main_channel&utm_campaign=dsa_new 💻 PreOrder Traversal: https://www.geeksforgeeks.org/problems/preorder-traversal/1?utm_source=youtube&utm_medium=main_channel&utm_campaign=dsa_new 💻 PostOrder Traversal: https://www.geeksforgeeks.org/problems/postorder-traversal/1?utm_source=youtube&utm_medium=main_channel&utm_campaign=dsa_new ------------------------------------------------------------------------- ⏰ Time Stamps: 0:00 - Introduction 0:20 - Basic Difference between InOrder, PreOrder and PostOrder Traversal 0:50 - Traversal in Binary Tree 5:23 - Trick to Remember Traversal order 6:18 - PreOrder Traversal Code 7:02 - PostOrder Traversal Code 7:38 - InOrder Traversal Code ------------------------------------------------------------------------- 🔴 Check out the video on Tree: https://youtu.be/AylOfzYJ2qE 🔴 Check out the Full DSA Course Playlist: https://youtube.com/playlist?list=PLqM7alHXFySHWUSBXUW9eq-ajb2JLoFNS&si=m_4B8bdS8dSJ9W6m ------------------------------------------------------------------------- 📚 Explore Our Courses: https://practice.geeksforgeeks.org/courses?utm_source=youtube&utm_medium=main_channel&utm_campaign=dsa_new 📖 Want to Learn more about Data Structures and Algorithms? Check out our Data Structures and Algorithms - Self Paced Course Now!: https://www.geeksforgeeks.org/courses/dsa-self-paced?utm_source=youtube&utm_medium=main_channel&utm_campaign=dsa_new -------------------------------
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from GeeksforGeeks · GeeksforGeeks · 0 of 60

← Previous Next →
1 How I got into Walmart | Shailesh Sharma
How I got into Walmart | Shailesh Sharma
GeeksforGeeks
2 Upgrade yourself In 29 Days | GeeksforGeeks
Upgrade yourself In 29 Days | GeeksforGeeks
GeeksforGeeks
3 Learn AWS Fundamentals For Free
Learn AWS Fundamentals For Free
GeeksforGeeks
4 Conversation With Young Achievers | Meet the winners of Bi-Wizard Coding Contest | GeeksforGeeks
Conversation With Young Achievers | Meet the winners of Bi-Wizard Coding Contest | GeeksforGeeks
GeeksforGeeks
5 Meet The Winners Of Bi-Wizard Coding Contests | GeeksforGeeks
Meet The Winners Of Bi-Wizard Coding Contests | GeeksforGeeks
GeeksforGeeks
6 Interview Prep Strategies | PayPal
Interview Prep Strategies | PayPal
GeeksforGeeks
7 OLX Interview Preparation Strategies | Hukam Singh
OLX Interview Preparation Strategies | Hukam Singh
GeeksforGeeks
8 Meet Some More Winners Of Bi-Wizard Coding Contests | GeeksforGeeks
Meet Some More Winners Of Bi-Wizard Coding Contests | GeeksforGeeks
GeeksforGeeks
9 Live Mock DSA
Live Mock DSA
GeeksforGeeks
10 Microsoft Azure For Absolute Beginners
Microsoft Azure For Absolute Beginners
GeeksforGeeks
11 Python for Data Science | Data Science Master Bootcamp | Arpit Jain
Python for Data Science | Data Science Master Bootcamp | Arpit Jain
GeeksforGeeks
12 Getting Started with Data Analysis | Data Science Master Bootcamp | Ashish Jangra
Getting Started with Data Analysis | Data Science Master Bootcamp | Ashish Jangra
GeeksforGeeks
13 How to prepare theory subjects for SDE interviews | Geeks Summer Carnival 2022
How to prepare theory subjects for SDE interviews | Geeks Summer Carnival 2022
GeeksforGeeks
14 Get Your Tickets To The Geeks Summer Carnival | GeeksforGeeks
Get Your Tickets To The Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
15 TED Talk Data Analysis Project | Data Science Master Bootcamp | Ashish Jangra
TED Talk Data Analysis Project | Data Science Master Bootcamp | Ashish Jangra
GeeksforGeeks
16 How I Secured AIR 9 in GATE'22 |  Tushar
How I Secured AIR 9 in GATE'22 | Tushar
GeeksforGeeks
17 Learn Java Backend Development | Geeks Summer Carnival | GeeksforGeeks
Learn Java Backend Development | Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
18 How to Recognize which Data Structure to use in a question | Geeks Summer Carnival | GeeksforGeeks
How to Recognize which Data Structure to use in a question | Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
19 Learn Data Structures and Algorithms | GeeksforGeeks
Learn Data Structures and Algorithms | GeeksforGeeks
GeeksforGeeks
20 Interview experience at Flipkart | GeeksforGeeks
Interview experience at Flipkart | GeeksforGeeks
GeeksforGeeks
21 Lets Prepare for GATE'23 the Right Way | Sakshi Singhal | GeekSummerCarnival
Lets Prepare for GATE'23 the Right Way | Sakshi Singhal | GeekSummerCarnival
GeeksforGeeks
22 Highest Paying Jobs in 2022 | Ishan Sharma | Geeks Summer Carnival 2022 | GeeksforGeeks
Highest Paying Jobs in 2022 | Ishan Sharma | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
23 Geeks Summer Carnival 2022 | 5th April- 11th April | GeeksforGeeks
Geeks Summer Carnival 2022 | 5th April- 11th April | GeeksforGeeks
GeeksforGeeks
24 Preparing for SDE interviews | Soham Mukherjee | Geeks Summer Carnival 2022 | GeeksforGeeks
Preparing for SDE interviews | Soham Mukherjee | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
25 Full Stack Development with React & Node | Utkarsh Malik | Geeks Summer Carnival | GeeksforGeeks
Full Stack Development with React & Node | Utkarsh Malik | Geeks Summer Carnival | GeeksforGeeks
GeeksforGeeks
26 Introduction to Open Source and Roadmap to GSOC 2022 | Geeks Summer Carnival 2022 | GeeksforGeeks
Introduction to Open Source and Roadmap to GSOC 2022 | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
27 Web Scraping in Action | Geeks Summer Carnival 2022 | GeeksforGeeks
Web Scraping in Action | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
28 Getting Hired at BITCS via GfG Job Portal | Get Hired With GeeksforGeeks
Getting Hired at BITCS via GfG Job Portal | Get Hired With GeeksforGeeks
GeeksforGeeks
29 How to build a faster landing Page | Geeks Summer Carnival 2022 | GeeksforGeeks
How to build a faster landing Page | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
30 Geeks Summer Carnival | 5th To 11th April, 2022 | GeeksforGeeks
Geeks Summer Carnival | 5th To 11th April, 2022 | GeeksforGeeks
GeeksforGeeks
31 How to get ideas for Startup | Geeks Summer Carnival 2022 | GeeksforGeeks
How to get ideas for Startup | Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
32 Journey from Tier 3 to JusPay | GeeksforGeeks
Journey from Tier 3 to JusPay | GeeksforGeeks
GeeksforGeeks
33 Geeks Summer Carnival 2022 | GeeksforGeeks
Geeks Summer Carnival 2022 | GeeksforGeeks
GeeksforGeeks
34 Dispelling Myths and Pre conceptions of Programming Languages
Dispelling Myths and Pre conceptions of Programming Languages
GeeksforGeeks
35 Must Do System Design Questions
Must Do System Design Questions
GeeksforGeeks
36 Understanding Sorting Techniques in an hour | Keerti Purswani | Geeks Summer Carnival
Understanding Sorting Techniques in an hour | Keerti Purswani | Geeks Summer Carnival
GeeksforGeeks
37 Get Hired at NEC | Job-A-Thon 8
Get Hired at NEC | Job-A-Thon 8
GeeksforGeeks
38 Journey from Tier 3 college to Microsoft | GeeksforGeeks
Journey from Tier 3 college to Microsoft | GeeksforGeeks
GeeksforGeeks
39 Get Hired with GeeksforGeeks at SuperK | Job A Thon 8
Get Hired with GeeksforGeeks at SuperK | Job A Thon 8
GeeksforGeeks
40 GeeksforGeeks: Redesigned
GeeksforGeeks: Redesigned
GeeksforGeeks
41 From Tier 3 to cracking multiple interviews | GeeksforGeeks
From Tier 3 to cracking multiple interviews | GeeksforGeeks
GeeksforGeeks
42 Live Mock DSA
Live Mock DSA
GeeksforGeeks
43 Youtube Data Analysis | Ashish Jangra | GeeksforGeeks
Youtube Data Analysis | Ashish Jangra | GeeksforGeeks
GeeksforGeeks
44 DSA Self-Paced Course Preview | Sandeep Jain | GeeksforGeeks
DSA Self-Paced Course Preview | Sandeep Jain | GeeksforGeeks
GeeksforGeeks
45 GATE Live Classes | Prepare for GATE CS 2023 | GeeksforGeeks
GATE Live Classes | Prepare for GATE CS 2023 | GeeksforGeeks
GeeksforGeeks
46 Journey from JIIT to Adobe
Journey from JIIT to Adobe
GeeksforGeeks
47 Life Is Unfair Ft. Shonty badmash | LIVE Discord Session | A GeeksforGeeks Exclusive
Life Is Unfair Ft. Shonty badmash | LIVE Discord Session | A GeeksforGeeks Exclusive
GeeksforGeeks
48 Interview Experience at Google | Tech Dose
Interview Experience at Google | Tech Dose
GeeksforGeeks
49 Live Mock DSA
Live Mock DSA
GeeksforGeeks
50 Interview Experience @ Amazon | GeeksforGeeks
Interview Experience @ Amazon | GeeksforGeeks
GeeksforGeeks
51 My journey through the tech world from India to US | Vidushi | GeeksforGeeks
My journey through the tech world from India to US | Vidushi | GeeksforGeeks
GeeksforGeeks
52 Complete Interview Preparation Course | GeeksforGeeks
Complete Interview Preparation Course | GeeksforGeeks
GeeksforGeeks
53 Live Mock DSA
Live Mock DSA
GeeksforGeeks
54 Getting Hired at FiftyFive Technologies | Job-a-thon 9.0
Getting Hired at FiftyFive Technologies | Job-a-thon 9.0
GeeksforGeeks
55 GFG Karlo, Ho Jayega | GeeksforGeeks ft. Khaleel Ahmed
GFG Karlo, Ho Jayega | GeeksforGeeks ft. Khaleel Ahmed
GeeksforGeeks
56 How I got job offers from 2 big companies : Arcesium & Microsoft | GeeksforGeeks
How I got job offers from 2 big companies : Arcesium & Microsoft | GeeksforGeeks
GeeksforGeeks
57 LINUX for Beginners | GFG x Itversity
LINUX for Beginners | GFG x Itversity
GeeksforGeeks
58 My interview experience at Walmart | GeeksforGeeks
My interview experience at Walmart | GeeksforGeeks
GeeksforGeeks
59 Get Hired at Speckyfox
Get Hired at Speckyfox
GeeksforGeeks
60 Live Mock DSA
Live Mock DSA
GeeksforGeeks

Related Reads

📰
Building a Power Grid Inside Minecraft with BFS Algorithms
Learn to build a power grid inside Minecraft using BFS algorithms and understand its relevance to cloud services
Dev.to · Carlos Cortez 🇵🇪 [AWS Hero]
📰
The Run-Length Encoding Trick: How Simple Strings Get Compressed
Learn how Run-Length Encoding (RLE) compresses simple strings, a fundamental technique in programming and data compression, and apply it to optimize storage and transmission of data
Medium · Programming
📰
75 Days of Leetcode — Day 4: #238 — Product of Array Except Self
Solve the Product of Array Except Self problem on LeetCode to improve coding skills and learn array manipulation techniques
Medium · AI
📰
I implemented the algorithm that broke the sorting barrier. Dijkstra still wins.
Learn how the author implemented an algorithm that broke the sorting barrier and compare its performance with Dijkstra's algorithm
Medium · Programming

Chapters (7)

Introduction
0:20 Basic Difference between InOrder, PreOrder and PostOrder Traversal
0:50 Traversal in Binary Tree
5:23 Trick to Remember Traversal order
6:18 PreOrder Traversal Code
7:02 PostOrder Traversal Code
7:38 InOrder Traversal Code
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →