Modify a binary tree to get preorder traversal using right pointers only
Last Updated :
02 Aug, 2022
Given a binary tree. Modify it in such a way that after modification you can have a preorder traversal of it using only the right pointers. During modification, you can use right as well as left pointers.
Examples:
Input : 10
/ \
8 2
/ \
3 5
Output : 10
\
8
\
3
\
5
\
2
Explanation : The preorder traversal
of given binary tree is 10 8 3 5 2.
Method 1 (Recursive):
One needs to make the right pointer of root point to the left subtree.
If the node has just left child, then just moving the child to right will complete the processing for that node.
If there is a right child too, then it should be made right child of the right-most of the original left subtree.
The above function used in the code process a node and then returns the rightmost node of the transformed subtree.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* newNode( int data)
{
struct Node* node = new struct Node;
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
struct Node* modifytree( struct Node* root)
{
struct Node* right = root->right;
struct Node* rightMost = root;
if (root->left) {
rightMost = modifytree(root->left);
root->right = root->left;
root->left = NULL;
}
if (!right)
return rightMost;
rightMost->right = right;
rightMost = modifytree(right);
return rightMost;
}
void printpre( struct Node* root)
{
while (root != NULL) {
cout << root->data << " " ;
root = root->right;
}
}
int main()
{
struct Node* root = newNode(10);
root->left = newNode(8);
root->right = newNode(2);
root->left->left = newNode(3);
root->left->right = newNode(5);
modifytree(root);
printpre(root);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node left;
Node right;
};
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
static Node modifytree( Node root)
{
Node right = root.right;
Node rightMost = root;
if (root.left != null )
{
rightMost = modifytree(root.left);
root.right = root.left;
root.left = null ;
}
if (right == null )
return rightMost;
rightMost.right = right;
rightMost = modifytree(right);
return rightMost;
}
static void printpre( Node root)
{
while (root != null )
{
System.out.print( root.data + " " );
root = root.right;
}
}
public static void main(String args[])
{
Node root = newNode( 10 );
root.left = newNode( 8 );
root.right = newNode( 2 );
root.left.left = newNode( 3 );
root.left.right = newNode( 5 );
modifytree(root);
printpre(root);
}
}
|
Python3
class newNode():
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def modifytree(root):
right = root.right
rightMost = root
if (root.left):
rightMost = modifytree(root.left)
root.right = root.left
root.left = None
if ( not right):
return rightMost
rightMost.right = right
rightMost = modifytree(right)
return rightMost
def printpre(root):
while (root ! = None ):
print (root.data,end = " " )
root = root.right
if __name__ = = '__main__' :
root = newNode( 10 )
root.left = newNode( 8 )
root.right = newNode( 2 )
root.left.left = newNode( 3 )
root.left.right = newNode( 5 )
modifytree(root)
printpre(root)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node left;
public Node right;
};
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
static Node modifytree( Node root)
{
Node right = root.right;
Node rightMost = root;
if (root.left != null )
{
rightMost = modifytree(root.left);
root.right = root.left;
root.left = null ;
}
if (right == null )
return rightMost;
rightMost.right = right;
rightMost = modifytree(right);
return rightMost;
}
static void printpre( Node root)
{
while (root != null )
{
Console.Write( root.data + " " );
root = root.right;
}
}
public static void Main(String []args)
{
Node root = newNode(10);
root.left = newNode(8);
root.right = newNode(2);
root.left.left = newNode(3);
root.left.right = newNode(5);
modifytree(root);
printpre(root);
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
function newNode(data)
{
let node = new Node(data);
return (node);
}
function modifytree(root)
{
let right = root.right;
let rightMost = root;
if (root.left != null )
{
rightMost = modifytree(root.left);
root.right = root.left;
root.left = null ;
}
if (right == null )
return rightMost;
rightMost.right = right;
rightMost = modifytree(right);
return rightMost;
}
function printpre(root)
{
while (root != null )
{
document.write( root.data + " " );
root = root.right;
}
}
let root = newNode(10);
root.left = newNode(8);
root.right = newNode(2);
root.left.left = newNode(3);
root.left.right = newNode(5);
modifytree(root);
printpre(root);
</script>
|
Time Complexity : O(n)
Auxiliary Space : O(n)
Method 2 (Iterative): This can be easily done using iterative preorder traversal. See here. Iterative preorder traversal
The idea is to maintain a variable prev which maintains the previous node of the preorder traversal. Every-time a new node is encountered, the node set its right to previous one and prev is made equal to the current node. In the end we will have a sort of linked list whose first element is root then left child then right, so on and so forth.
Implementation:
C++
#include <iostream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* newNode( int data)
{
struct Node* node = new struct Node;
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
void modifytree( struct Node* root)
{
if (root == NULL)
return ;
stack<Node*> nodeStack;
nodeStack.push(root);
struct Node* pre = NULL;
while (nodeStack.empty() == false ) {
struct Node* node = nodeStack.top();
nodeStack.pop();
if (node->right)
nodeStack.push(node->right);
if (node->left)
nodeStack.push(node->left);
if (pre != NULL) {
pre->right = node;
}
pre = node;
}
}
void printpre( struct Node* root)
{
while (root != NULL) {
cout << root->data << " " ;
root = root->right;
}
}
int main()
{
struct Node* root = newNode(10);
root->left = newNode(8);
root->right = newNode(2);
root->left->left = newNode(3);
root->left->right = newNode(5);
modifytree(root);
printpre(root);
return 0;
}
|
Java
import java.util.*;
class GfG {
static class Node {
int data;
Node left;
Node right;
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
static void modifytree(Node root)
{
if (root == null )
return ;
Stack<Node> nodeStack = new Stack<Node> ();
nodeStack.push(root);
Node pre = null ;
while (nodeStack.isEmpty() == false ) {
Node node = nodeStack.peek();
nodeStack.pop();
if (node.right != null )
nodeStack.push(node.right);
if (node.left != null )
nodeStack.push(node.left);
if (pre != null ) {
pre.right = node;
}
pre = node;
}
}
static void printpre(Node root)
{
while (root != null ) {
System.out.print(root.data + " " );
root = root.right;
}
}
public static void main(String[] args)
{
Node root = newNode( 10 );
root.left = newNode( 8 );
root.right = newNode( 2 );
root.left.left = newNode( 3 );
root.left.right = newNode( 5 );
modifytree(root);
printpre(root);
}
}
|
Python
class newNode():
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def modifytree( root):
if (root = = None ):
return
nodeStack = []
nodeStack.append(root)
pre = None
while ( len (nodeStack)):
node = nodeStack[ - 1 ]
nodeStack.pop()
if (node.right):
nodeStack.append(node.right)
if (node.left):
nodeStack.append(node.left)
if (pre ! = None ):
pre.right = node
pre = node
def printpre( root):
while (root ! = None ):
print (root.data, end = " " )
root = root.right
root = newNode( 10 )
root.left = newNode( 8 )
root.right = newNode( 2 )
root.left.left = newNode( 3 )
root.left.right = newNode( 5 )
modifytree(root)
printpre(root)
|
C#
using System;
using System.Collections;
class GfG
{
public class Node
{
public int data;
public Node left;
public Node right;
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
static void modifytree(Node root)
{
if (root == null )
return ;
Stack nodeStack = new Stack();
nodeStack.Push(root);
Node pre = null ;
while (nodeStack.Count !=0)
{
Node node = (Node)nodeStack.Peek();
nodeStack.Pop();
if (node.right != null )
nodeStack.Push(node.right);
if (node.left != null )
nodeStack.Push(node.left);
if (pre != null )
{
pre.right = node;
}
pre = node;
}
}
static void printpre(Node root)
{
while (root != null )
{
Console.Write(root.data + " " );
root = root.right;
}
}
public static void Main(String []args)
{
Node root = newNode(10);
root.left = newNode(8);
root.right = newNode(2);
root.left.left = newNode(3);
root.left.right = newNode(5);
modifytree(root);
printpre(root);
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
function newNode(data)
{
let node = new Node(data);
return (node);
}
function modifytree(root)
{
if (root == null )
return ;
let nodeStack = [];
nodeStack.push(root);
let pre = null ;
while (nodeStack.length > 0) {
let node = nodeStack[nodeStack.length - 1];
nodeStack.pop();
if (node.right != null )
nodeStack.push(node.right);
if (node.left != null )
nodeStack.push(node.left);
if (pre != null ) {
pre.right = node;
}
pre = node;
}
}
function printpre(root)
{
while (root != null ) {
document.write(root.data + " " );
root = root.right;
}
}
let root = newNode(10);
root.left = newNode(8);
root.right = newNode(2);
root.left.left = newNode(3);
root.left.right = newNode(5);
modifytree(root);
printpre(root);
</script>
|
Time Complexity : O(n)
Auxiliary Space : O(n)
Please Login to comment...