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++
using namespace std;
#include <iostream>
#include <stdlib.h>
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;
}
void PrintInorderBinaryTree(Node* root)
{
if (root == NULL)
return ;
PrintInorderBinaryTree(root->left);
std::cout << root->data << " " ;
PrintInorderBinaryTree(root->right);
}
void FlattenBinaryTree(Node* root, Node** last)
{
if (root == NULL)
return ;
Node* left = root->left;
Node* right = root->right;
if (root != *last) {
(*last)->right = root;
(*last)->left = NULL;
*last = root;
}
FlattenBinaryTree(left, last);
FlattenBinaryTree(right, last);
if (left == NULL && right == NULL)
*last = root;
}
int main()
{
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);
std::cout << "Original inorder traversal : " ;
PrintInorderBinaryTree(root);
std::cout << std::endl;
Node* last = root;
FlattenBinaryTree(root, &last);
std::cout << "Flattened inorder traversal : " ;
PrintInorderBinaryTree(root);
std::cout << std::endl;
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node left;
Node right;
};
static Node AllocNode( int data)
{
Node temp = new Node();
temp.left = null ;
temp.right = null ;
temp.data = data;
return temp;
}
static void PrintInorderBinaryTree(Node root)
{
if (root == null )
return ;
PrintInorderBinaryTree(root.left);
System.out.print( root.data + " " );
PrintInorderBinaryTree(root.right);
}
static Node last = null ;
static void FlattenBinaryTree(Node root)
{
if (root == null )
return ;
Node left = root.left;
Node right = root.right;
if (root != last) {
(last).right = root;
(last).left = null ;
last = root;
}
FlattenBinaryTree(left);
FlattenBinaryTree(right);
if (left == null && right == null )
last = root;
}
public static void main(String args[])
{
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 );
System.out.print( "Original inorder traversal : " );
PrintInorderBinaryTree(root);
System.out.println();
last = root;
FlattenBinaryTree(root);
System.out.print( "Flattened inorder traversal : " );
PrintInorderBinaryTree(root);
System.out.println();
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self .right = None
self .left = None
def PrintInorderBinaryTree(root):
if (root = = None ):
return
PrintInorderBinaryTree(root.left)
print ( str (root.data), end = " " )
PrintInorderBinaryTree(root.right)
def FlattenBinaryTree(root):
global last
if (root = = None ):
return
left = root.left
right = root.right
if (root ! = last):
last.right = root
last.left = None
last = root
FlattenBinaryTree(left)
FlattenBinaryTree(right)
if (left = = None and right = = None ):
last = root
root = Node( 1 )
root.left = Node( 2 )
root.left.left = Node( 3 )
root.left.right = Node( 4 )
root.right = Node( 5 )
root.right.right = Node( 6 )
print ( "Original inorder traversal : " , end = "")
PrintInorderBinaryTree(root)
print ("")
last = root
FlattenBinaryTree(root)
print ( "Flattened inorder traversal : " , end = "")
PrintInorderBinaryTree(root)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node left;
public Node right;
};
static Node AllocNode( int data)
{
Node temp = new Node();
temp.left = null ;
temp.right = null ;
temp.data = data;
return temp;
}
static void PrintInorderBinaryTree(Node root)
{
if (root == null )
return ;
PrintInorderBinaryTree(root.left);
Console.Write(root.data + " " );
PrintInorderBinaryTree(root.right);
}
static Node last = null ;
static void FlattenBinaryTree(Node root)
{
if (root == null )
return ;
Node left = root.left;
Node right = root.right;
if (root != last)
{
(last).right = root;
(last).left = null ;
last = root;
}
FlattenBinaryTree(left);
FlattenBinaryTree(right);
if (left == null && right == null )
last = root;
}
public static void Main(String []args)
{
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);
Console.Write( "Original inorder traversal : " );
PrintInorderBinaryTree(root);
Console.WriteLine();
last = root;
FlattenBinaryTree(root);
Console.Write( "Flattened inorder traversal : " );
PrintInorderBinaryTree(root);
Console.WriteLine();
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .left = null ;
this .right = null ;
}
}
function AllocNode( data)
{
var temp = new Node();
temp.left = null ;
temp.right = null ;
temp.data = data;
return temp;
}
function PrintInorderBinaryTree( root)
{
if (root == null )
return ;
PrintInorderBinaryTree(root.left);
document.write( root.data + " " );
PrintInorderBinaryTree(root.right);
}
var last = null ;
function FlattenBinaryTree( root)
{
if (root == null )
return ;
var left = root.left;
var right = root.right;
if (root != last) {
(last).right = root;
(last).left = null ;
last = root;
}
FlattenBinaryTree(left);
FlattenBinaryTree(right);
if (left == null && right == null )
last = root;
}
var 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);
document.write( "Original inorder traversal : " );
PrintInorderBinaryTree(root);
document.write( "</br>" );
last = root;
FlattenBinaryTree(root);
document.write( "Flattened inorder traversal : " );
PrintInorderBinaryTree(root);
document.write( "</br>" );
</script>
|
Output
Original inorder traversal : 3 2 4 1 5 6
Flattened inorder traversal : 1 2 3 4 5 6
Time Complexity: O(N) where N is the number of nodes in the binary tree.
Auxiliary Space: O(h) where h is the height of binary tree due to recursion call stack.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Jan, 2023
Like Article
Save Article