Given a tree and a node, the task is to reverse the path till the given Node and print the in-order traversal of the modified tree.
Input: 7 / \ 6 5 / \ / \ 4 3 2 1 Node = 4 Output: 7 6 3 4 2 5 1 The path from root to node 4 is 7 -> 6 -> 4 Reversing this path, the modified tree will be: 4 / \ 6 5 / \ / \ 7 3 2 1 whose in-order traversal is 7 6 3 4 2 5 1 Input: 7 / \ 6 5 / \ / \ 4 3 2 1 Node = 2 Output: 4 6 3 2 7 5 1
- First store all the nodes on the given path in a queue.
- If the key is found then replace this node data with front of queue data and pop the front.
- Keep on performing this operation in the recursive way upto the root and the path will be reversed in the original tree.
- Now, print the in-order traversal of the modified tree.
Below is the implementation of the above approach:
7 6 3 4 2 5 1
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 a path in BST using queue
- Reverse tree path
- Minimum cost to reverse edges such that there is path between every pair of nodes
- Right view of Binary Tree using Queue
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- Reverse zigzag Traversal of a Binary Tree
- Maximum Path Sum in a Binary Tree
- Check if two nodes are on same path in a tree | Set 2
- XOR of path between any two nodes in a Binary Tree
- Longest path in an undirected tree
- Count of 1's in any path in a Binary Tree
- Maximum XOR path of a Binary Tree
- GCD from root to leaf path in an N-ary tree
- Find maximum and minimum element in binary tree without using recursion or stack or queue
- Reverse Clockwise spiral traversal of a binary tree
- Reverse alternate levels of a perfect binary tree
- Reverse Morris traversal using Threaded Binary Tree
- Minimum sum path between two leaves of a binary tree
- Print all Coprime path of a Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.