Flatten a binary tree into linked list | Set-3
Given a binary tree, flatten it into linked list in-place. Usage of auxiliary data structure is not allowed. After flattening, left of each node should point to NULL and right should contain next node in level order.
Examples:
Input:
1
/ \
2 5
/ \ \
3 4 6
Output:
1
\
2
\
3
\
4
\
5
\
6
Input:
1
/ \
3 4
/
2
\
5
Output:
1
\
3
\
4
\
2
\
5
Approach: Recurse the binary tree in Inorder Format, at every stage of function call pass on the address of last node in the flattened linked list so that current node can make itself a right node of the last node.
For left child, it’s parent node is the last node in the flattened list
For the right child there are two conditions:
- If there is no left child to the parent, parent node is the last node in the flattened list.
- If left child is not null then leaf node from left sub-tree is the last node in the flattened list.
Below is the implementation of the above approach:
// C++ program to flatten the binary tree // using previous node approach using namespace std; #include <iostream> #include <stdlib.h> // Structure to represent a node of the tree struct Node { int data; struct Node* left; struct Node* right; }; Node* AllocNode(int data) { Node* temp = new Node; temp->left = NULL; temp->right = NULL; temp->data = data; return temp; } // Utility function to print the inorder // traversal of the tree void PrintInorderBinaryTree(Node* root) { if (root == NULL) return; PrintInorderBinaryTree(root->left); std::cout << root->data << " "; PrintInorderBinaryTree(root->right); } // Function to make current node right of // the last node in the list void FlattenBinaryTree(Node* root, Node** last) { if (root == NULL) return; Node* left = root->left; Node* right = root->right; // Avoid first iteration where root is // the only node in the list if (root != *last) { (*last)->right = root; (*last)->left = NULL; *last = root; } FlattenBinaryTree(left, last); FlattenBinaryTree(right, last); if (left == NULL && right == NULL) *last = root; } // Driver Code int main() { // Build the tree Node* root = AllocNode(1); root->left = AllocNode(2); root->left->left = AllocNode(3); root->left->right = AllocNode(4); root->right = AllocNode(5); root->right->right = AllocNode(6); // Print the inorder traversal of the // original tree std::cout << "Original inorder traversal : "; PrintInorderBinaryTree(root); std::cout << std::endl; // Flatten a binary tree, at the beginning // root node is the only and last in the list Node* last = root; FlattenBinaryTree(root, &last;); // Print the inorder traversal of the flattened // binary tree std::cout << "Flattened inorder traversal : "; PrintInorderBinaryTree(root); std::cout << std::endl; return 0; } |
Original inorder traversal : 3 2 4 1 5 6 Flattened inorder traversal : 1 2 3 4 5 6
Recommended Posts:
- Flatten a binary tree into linked list | Set-2
- Flatten a binary tree into linked list
- Flatten a multilevel linked list
- Flatten a multi-level linked list | Set 2 (Depth wise)
- Convert a given Binary Tree to Doubly Linked List | Set 3
- Convert a given Binary Tree to Doubly Linked List | Set 2
- Convert a given Binary Tree to Doubly Linked List | Set 4
- Convert a given Binary Tree to Doubly Linked List | Set 1
- Construct Complete Binary Tree from its Linked List Representation
- Extract Leaves of a Binary Tree in a Doubly Linked List
- Convert a Binary Tree into Doubly Linked List in spiral fashion
- Binary Search on Singly Linked List
- Decimal Equivalent of Binary Linked List
- Create a Doubly Linked List from a Ternary Tree
- Linked complete binary tree & its creation
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.



