Flatten binary tree in order of post-order traversal
Given a binary tree, the task is to flatten it in order of its post-order traversal. In the flattened binary tree, the left node of all the nodes must be NULL.
Examples:
Input:
5
/ \
3 7
/ \ / \
2 4 6 8
Output: 2 4 3 6 8 7 5
Input:
1
\
2
\
3
\
4
\
5
Output: 5 4 3 2 1
A simple approach will be to recreate the Binary Tree from its post-order traversal. This will take O(N) extra space were N is the number of nodes in BST.
A better solution is to simulate post-order traversal of the given binary tree.
- Create a dummy node.
- Create variable called ‘prev’ and make it point to the dummy node.
- Perform post-order traversal and at each step.
- Set prev -> right = curr
- Set prev -> left = NULL
- Set prev = curr
This will improve the space complexity to O(H) in the worst case as post-order traversal takes O(H) extra space.
Below is the implementation of the above approach:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Node of the binary tree struct node { int data; node* left; node* right; node(int data) { this->data = data; left = NULL; right = NULL; } }; // Function to print the flattened // binary Tree void print(node* parent) { node* curr = parent; while (curr != NULL) cout << curr->data << " ", curr = curr->right; } // Function to perform post-order traversal // recursively void postorder(node* curr, node*& prev) { // Base case if (curr == NULL) return; postorder(curr->left, prev); postorder(curr->right, prev); prev->left = NULL; prev->right = curr; prev = curr; } // Function to flatten the given binary tree // using post order traversal node* flatten(node* parent) { // Dummy node node* dummy = new node(-1); // Pointer to previous element node* prev = dummy; // Calling post-order traversal postorder(parent, prev); prev->left = NULL; prev->right = NULL; node* ret = dummy->right; // Delete dummy node delete dummy; return ret; } // Driver code int main() { node* root = new node(5); root->left = new node(3); root->right = new node(7); root->left->left = new node(2); root->left->right = new node(4); root->right->left = new node(6); root->right->right = new node(8); print(flatten(root)); return 0; } |
2 4 3 6 8 7 5
Recommended Posts:
- Flatten Binary Tree in order of Level Order Traversal
- Flatten Binary Tree in order of Zig Zag traversal
- Postorder traversal of Binary Tree without recursion and without stack
- Find n-th node in Postorder traversal of a Binary Tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Density of Binary Tree using Level Order Traversal
- Find the kth node in vertical order traversal of a Binary Tree
- Perfect Binary Tree Specific Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Insertion in n-ary tree in given order and Level order traversal
- Flatten a binary tree into linked list | Set-3
- Flatten a binary tree into linked list | Set-2
- Flatten a binary tree into linked list
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.



