# Sort the path from root to a given node in a Binary Tree

Given a Binary tree, the task is to sort the particular path from to a given node of the binary tree. You are given a Key Node and Tree. The task is to sort the path till that particular node.

Examples:

Input :
3
/   \
4     5
/ \     \
1   2     6
key = 2
Output :
2
/   \
3     5
/ \     \
1   4     6
Inorder :- 1 3 4 2 5 6
Here the path from root to given key is sorted
from 3(root) to 2(key).

Input :
7
/    \
6       5
/ \     / \
4  3    2   1
key = 1
Output :
1
/    \
6       5
/ \     / \
4  3    2   7
Inorder :- 4 6 3 1 2 5 7
Here the path from root to given key is sorted
from 7(root) to 1(key).

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Algorithm: Following is simple algorithm to sort the path top to bottom (increasing order).

1. Find path from root to given key node and store it in a priority queue.
2. Replace the value of node with the priority queue top element.
3. if left pq size is greater than replace the value of node with left pq after pop out the element.
4. if right pq size is greater then replace the value of node with right pq after pop out the element.
5. Print the tree in inorder.

Below is the implementation of the above approach:

 // CPP program to sort the path from root to // given node of a binary tree    #include #include using namespace std;       // Binary Tree node struct Node {     int data; // store data     Node *left, *right; // left right pointer };    /* utility that allocates a new Node  with the given key */ Node* newNode(int data) {     Node* node = new Node;     node->data = data;     node->left = node->right = NULL;     return (node); }    // Function to find the inorder traversal void inorder(struct Node* root) {     // base condition     if (root == NULL)         return;        // go to left part     inorder(root->left);        // print the data     cout << root->data << " ";        // go to right part     inorder(root->right); }    priority_queue solUtil(Node* root, int key,                              priority_queue pq) {     priority_queue blank;        // if node is not found     // then we will return     // blank priority queue     if (root == NULL)         return blank;        // store the path in priority queue     pq.push(root->data);        // Go to left subtree to store the left path node data     priority_queue left = solUtil(root->left, key, pq);        // Go to right subtree to store the right path node data     priority_queue right = solUtil(root->right, key, pq);        // if the key is found then     if (root->data == key) {         root->data = pq.top();         pq.pop();         return pq;     }     else if (left.size()) // if the node in path then     { // we change the root node data         root->data = left.top();         left.pop();         return left;     }     else if (right.size()) // if the node in path then     { // we change the root node data         root->data = right.top();         right.pop();         return right;     }        // if no key node found     // then return blank     // priority_queue     return blank; }    // Function to sort path from  // root to a given key node void sortPath(Node* root, int key) {     // for store the data     // in a sorted manner     priority_queue pq;        // call the solUtil function     // sort the path     solUtil(root, key, pq); }    // Driver Code int main() {     /*   3          / \        4       5       / \    \      1   2     6 */        // Build the tree     // given data     Node* root = newNode(3);     root->left = newNode(4);     root->right = newNode(5);     root->left->left = newNode(1);     root->left->right = newNode(2);     root->right->right = newNode(6);        // given key     int key = 1;        // Call the function to     // sort the path till given key tree     sortPath(root, key);        // call the function to print tree     inorder(root);        return 0; }

Output:-

1 3 4 2 5 6

Time Complexity: O(N logN) [N for traversing all the node and N*logN for priority queue]

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.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.