Given a binary search tree and a key, your task to reverse path of the binary tree.
Prerequisite : Reverse path of Binary tree
Input : 50 / \ 30 70 / \ / \ 20 40 60 80 k = 70 Output : Inorder before reversal : 20 30 40 50 60 70 80 Inorder after reversal : 20 30 40 70 60 50 80 Input : 8 / \ 3 10 / \ \ 1 6 14 / \ / 4 7 13 k = 13 Output : Inorder before reversal : 1 3 4 6 7 8 10 13 14 Inorder after reversal : 1 3 4 6 7 13 14 10 8
Take a queue and push all the element till that given key at the end replace node key with queue front element till root, then print inorder of the tree.
Below is the implementation of above approach :
Before Reverse : 20 30 40 50 60 70 80 After Reverse : 20 30 40 80 60 70 50
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Reverse tree path using Queue
- Check if a queue can be sorted into another queue using a stack
- Stack and Queue in Python using queue Module
- queue::front() and queue::back() in C++ STL
- queue::push() and queue::pop() in C++ STL
- Reversing a Queue using another Queue
- Reversing a Queue
- Queue in Python
- queue::emplace() in C++ STL
- queue::swap() in C++ STL
- Queue using Stacks
- Priority Queue | Set 1 (Introduction)
- Applications of Priority Queue
- Queue of Pairs in C++ STL with Examples
- Reversing a queue using recursion
- Sharing a queue among three threads
- Reversing the first K elements of a Queue
- Queue Interface In Java
- Sort the Queue using Recursion
- Priority Queue in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : PranchalKatiyar