Flatten a binary tree into linked list | Set-2
Last Updated :
30 Aug, 2022
Given a binary tree, flatten it into a linked list. After flattening, the left of each node should point to NULL and right should contain next node in level order.
Example:
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:
An approach using recursion has already been discussed in the previous post. A pre-order traversal of the binary tree using stack has been implied in this approach. In this traversal, every time a right child is pushed in the stack, the right child is made equal to the left child and left child is made equal to NULL. If the right child of the node becomes NULL, the stack is popped and the right child becomes the popped value from the stack. The above steps are repeated until the size of the stack is zero or root is NULL.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <stack>
using namespace std;
struct Node {
int key;
Node *left, *right;
};
Node* newNode( int key)
{
Node* node = new Node;
node->key = key;
node->left = node->right = NULL;
return (node);
}
void inorder( struct Node* root)
{
if (root == NULL)
return ;
inorder(root->left);
cout << root->key << " " ;
inorder(root->right);
}
Node* solution(Node* A)
{
stack<Node*> st;
Node* ans = A;
while (A != NULL || st.size() != 0) {
if (A->right != NULL) {
st.push(A->right);
}
A->right = A->left;
A->left = NULL;
if (A->right == NULL && st.size() != 0) {
A->right = st.top();
st.pop();
}
A = A->right;
}
return ans;
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(5);
root->left->left = newNode(3);
root->left->right = newNode(4);
root->right->right = newNode(6);
root = solution(root);
cout << "The Inorder traversal after "
"flattening binary tree " ;
inorder(root);
return 0;
return 0;
}
|
Java
import java.util.Stack;
class GFG
{
static class Node
{
int key;
Node left, right;
}
static Node newNode( int key)
{
Node node = new Node();
node.key = key;
node.left = node.right = null ;
return (node);
}
static void inorder(Node root)
{
if (root == null )
return ;
inorder(root.left);
System.out.print(root.key + " " );
inorder(root.right);
}
static Node solution(Node A)
{
Stack<Node> st = new Stack<>();
Node ans = A;
while (A != null || st.size() != 0 )
{
if (A.right != null )
{
st.push(A.right);
}
A.right = A.left;
A.left = null ;
if (A.right == null && st.size() != 0 )
{
A.right = st.peek();
st.pop();
}
A = A.right;
}
return ans;
}
public static void main(String[] args)
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 5 );
root.left.left = newNode( 3 );
root.left.right = newNode( 4 );
root.right.right = newNode( 6 );
root = solution(root);
System.out.print( "The Inorder traversal after "
+ "flattening binary tree " );
inorder(root);
}
}
|
Python3
class Node:
def __init__( self , key):
self .key = key
self .left = None
self .right = None
def newNode(key):
node = Node(key)
node.key = key
node.left = node.right = None
return (node)
def inorder(root):
if (root = = None ):
return
inorder(root.left)
print (root.key, end = ' ' )
inorder(root.right)
def solution(A):
st = []
ans = A
while (A ! = None or len (st) ! = 0 ):
if (A.right ! = None ):
st.append(A.right)
A.right = A.left
A.left = None
if (A.right = = None and len (st) ! = 0 ):
A.right = st[ - 1 ]
st.pop()
A = A.right
return ans
if __name__ = = '__main__' :
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 5 )
root.left.left = newNode( 3 )
root.left.right = newNode( 4 )
root.right.right = newNode( 6 )
root = solution(root)
print ( "The Inorder traversal after "
"flattening binary tree " ,
end = '')
inorder(root)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int key;
public Node left, right;
}
static Node newNode( int key)
{
Node node = new Node();
node.key = key;
node.left = node.right = null ;
return (node);
}
static void inorder(Node root)
{
if (root == null )
return ;
inorder(root.left);
Console.Write(root.key + " " );
inorder(root.right);
}
static Node solution(Node A)
{
Stack<Node> st = new Stack<Node>();
Node ans = A;
while (A != null || st.Count != 0)
{
if (A.right != null )
{
st.Push(A.right);
}
A.right = A.left;
A.left = null ;
if (A.right == null && st.Count != 0)
{
A.right = st.Peek();
st.Pop();
}
A = A.right;
}
return ans;
}
public static void Main(String[] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(5);
root.left.left = newNode(3);
root.left.right = newNode(4);
root.right.right = newNode(6);
root = solution(root);
Console.Write( "The Inorder traversal after "
+ "flattening binary tree " );
inorder(root);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .key = 0;
this .left = null ;
this .right = null ;
}
}
function newNode(key)
{
var node = new Node();
node.key = key;
node.left = node.right = null ;
return (node);
}
function inorder( root)
{
if (root == null )
return ;
inorder(root.left);
document.write(root.key + " " );
inorder(root.right);
}
function solution(A)
{
var st = [];
var ans = A;
while (A != null || st.length != 0)
{
if (A.right != null )
{
st.push(A.right);
}
A.right = A.left;
A.left = null ;
if (A.right == null && st.length != 0)
{
A.right = st[st.length-1];
st.pop();
}
A = A.right;
}
return ans;
}
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(5);
root.left.left = newNode(3);
root.left.right = newNode(4);
root.right.right = newNode(6);
root = solution(root);
document.write( "The Inorder traversal after "
+ "flattening binary tree " );
inorder(root);
</script>
|
Output
The Inorder traversal after flattening binary tree 1 2 3 4 5 6
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(Log N)
Share your thoughts in the comments
Please Login to comment...