Reverse tree path using Queue
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.
Examples:
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
Approach:
- 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:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // A Binary Tree Node struct Node { int data; struct Node *left, *right; }; // Function to reverse the tree path queue< int > reverseTreePathUtil(Node* root, int data, queue< int > q1) { queue< int > emptyQueue; // If root is null then return // an empty queue if (root == NULL) return emptyQueue; // If the node is found if (root->data == data) { // Replace it with the queue's front q1.push(root->data); root->data = q1.front(); q1.pop(); return q1; } // Push data into the queue for // storing data from start to end q1.push(root->data); // If the returned queue is empty then // it means that the left sub-tree doesn't // contain the required node queue< int > left = reverseTreePathUtil(root->left, data, q1); // If the returned queue is empty then // it means that the right sub-tree doesn't // contain the required node queue< int > right = reverseTreePathUtil(root->right, data, q1); // If the required node is found // in the right sub-tree if (!right.empty()) { // Replace with the queue's front root->data = right.front(); right.pop(); return right; } // If the required node is found // in the right sub-tree if (!left.empty()) { // Replace with the queue's front root->data = left.front(); left.pop(); return left; } // Return emptyQueue if path // is not found return emptyQueue; } // Function to call reverseTreePathUtil // to reverse the tree path void reverseTreePath(Node* root, int data) { queue< int > q1; // reverse tree path reverseTreePathUtil(root, data, q1); } // Function to print the in-order // traversal of the tree void inorder(Node* root) { if (root != NULL) { inorder(root->left); cout << root->data << " " ; inorder(root->right); } } // Utility function to create a new tree node Node* newNode( int data) { Node* temp = new Node; temp->data = data; temp->left = temp->right = NULL; return temp; } // Driver code int main() { Node* root = newNode(7); root->left = newNode(6); root->right = newNode(5); root->left->left = newNode(4); root->left->right = newNode(3); root->right->left = newNode(2); root->right->right = newNode(1); int data = 4; // Function call to reverse the path reverseTreePath(root, data); // Print the in-order traversal // of the modified tree inorder(root); return 0; } |
chevron_right
filter_none
Output:
7 6 3 4 2 5 1
Recommended Posts:
- 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
- Zig Zag Level order traversal of a tree using single queue
- Longest path in an undirected tree
- Maximum Path Sum in a Binary Tree
- XOR of path between any two nodes in a Binary Tree
- GCD from root to leaf path in an N-ary tree
- Longest Path with Same Values in a Binary Tree
- Maximum XOR with given value in the path from root to given node in the tree
- Print path between any two nodes in a Binary Tree
- Minimum sum path between two leaves 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 contribute@geeksforgeeks.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.