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 8 10
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Reverse tree path using Queue
- Stack and Queue in Python using queue Module
- Check if a queue can be sorted into another queue using a stack
- Reversing a Queue using another Queue
- Two nodes of a BST are swapped, correct the BST | Set-2
- K'th Largest Element in BST when modification to BST is not allowed
- Find k-th smallest element in BST (Order Statistics in BST)
- Convert a normal BST to Balanced BST
- Two nodes of a BST are swapped, correct the BST
- queue::push() and queue::pop() in C++ STL
- queue::front() and queue::back() in C++ STL
- Difference between Circular Queue and Priority Queue
- Root to leaf path sum equal to a given number in BST
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- K'th smallest element in BST using O(1) Extra Space
- Count inversions in an array | Set 2 (Using Self-Balancing BST)
- Convert BST into a Min-Heap without using array
- K'th Largest element in BST using constant extra space
- Number of elements smaller than root using preorder traversal of a BST
- Construct a BST from given postorder traversal using Stack
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.