Reverse alternate levels of a perfect binary tree
PROBLEM :
Write a function to reverse alternate levels of a perfect binary tree.
For example
Given tree:
a
/ \
b c
/ \ / \
d e f g
/ \ / \ / \ / \
h i j k l m n o
Modified tree:
a
/ \
c b
/ \ / \
d e f g
/ \ / \ / \ / \
o n m l k j i h
1 should be Modified to 1
/ \ - - - - - - - - - - - - > / \
2 3 3 2
Inorder traversal : 3 1 2
Input:
The task is to complete the method which takes one argument, root of the Tree. The struct node has a data part which stores the data, pointer to left child and pointer to right child.
There are multiple test cases. For each test case, this method will be called individually.
Output:
The function should modify the root of tree such that the alternate levels of the binary tree are reversed .The output generated for the expected output will be the inorder traversal of the modified tree.
Constraints:
1 <=T<= 30
1 <=Number of nodes<= 100
1 <=Data of a node<= 1000
Example:
Input:
2
2
1 2 R 1 3 L
6
1 2 L 1 3 R 2 42 L 2 51 R 3 63 L 3 72 R
Output:
2 1 3
42 3 51 1 63 2 72
There are two test casess. First case represents a tree with 3 nodes and 2 edges where root is 1, left child of 1 is 3 and right child of 1 is 2. Second test case represents a tree with 6 edges and 7 nodes.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* A binary tree node
struct node
{
int data;
struct Node* left, * right;
}; */
int find_height(struct node *) ;
void tree_to_array(struct node *,int [],int *,int ) ;
void array_to_tree(struct node *,int [],int *,int ) ;
void reverseAlternate(struct node *root)
{
if(root==NULL)
return ;
int height ;
height=find_height(root) ;
int i,k,j ;
int arr[100] ;
for(i=0;i<height;i++)
{
if(i%2==0)
continue ;
else
{
k=0 ;
tree_to_array(root,arr,&k,i) ;
array_to_tree(root,arr,&k,i) ;
}
}
}
int find_height(struct node *root)
{
if(root==NULL)
return 0 ;
int L,R ;
L=find_height(root->left) ;
R=find_height(root->right) ;
return L>R ? L+1 : R+1 ;
}
void tree_to_array(struct node *root,int arr[],int *k,int level)
{
if(root==NULL)
return ;
if(level==0)
arr[(*k)++]=root->data ;
tree_to_array(root->left,arr,&(*k),level-1) ;
tree_to_array(root->right,arr,&(*k),level-1) ;
}
void array_to_tree(struct node *root,int arr[],int *k,int level)
{
if(root==NULL)
return ;
if(level==0)
root->data=arr[--(*k)] ;
array_to_tree(root->left,arr,&(*k),level-1) ;
array_to_tree(root->right,arr,&(*k),level-1) ;
}
---------------------------------------------------------------------------------
Write a function to reverse alternate levels of a perfect binary tree.
For example
Given tree:
a
/ \
b c
/ \ / \
d e f g
/ \ / \ / \ / \
h i j k l m n o
Modified tree:
a
/ \
c b
/ \ / \
d e f g
/ \ / \ / \ / \
o n m l k j i h
1 should be Modified to 1
/ \ - - - - - - - - - - - - > / \
2 3 3 2
Inorder traversal : 3 1 2
Input:
The task is to complete the method which takes one argument, root of the Tree. The struct node has a data part which stores the data, pointer to left child and pointer to right child.
There are multiple test cases. For each test case, this method will be called individually.
Output:
The function should modify the root of tree such that the alternate levels of the binary tree are reversed .The output generated for the expected output will be the inorder traversal of the modified tree.
Constraints:
1 <=T<= 30
1 <=Number of nodes<= 100
1 <=Data of a node<= 1000
Example:
Input:
2
2
1 2 R 1 3 L
6
1 2 L 1 3 R 2 42 L 2 51 R 3 63 L 3 72 R
Output:
2 1 3
42 3 51 1 63 2 72
There are two test casess. First case represents a tree with 3 nodes and 2 edges where root is 1, left child of 1 is 3 and right child of 1 is 2. Second test case represents a tree with 6 edges and 7 nodes.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* A binary tree node
struct node
{
int data;
struct Node* left, * right;
}; */
int find_height(struct node *) ;
void tree_to_array(struct node *,int [],int *,int ) ;
void array_to_tree(struct node *,int [],int *,int ) ;
void reverseAlternate(struct node *root)
{
if(root==NULL)
return ;
int height ;
height=find_height(root) ;
int i,k,j ;
int arr[100] ;
for(i=0;i<height;i++)
{
if(i%2==0)
continue ;
else
{
k=0 ;
tree_to_array(root,arr,&k,i) ;
array_to_tree(root,arr,&k,i) ;
}
}
}
int find_height(struct node *root)
{
if(root==NULL)
return 0 ;
int L,R ;
L=find_height(root->left) ;
R=find_height(root->right) ;
return L>R ? L+1 : R+1 ;
}
void tree_to_array(struct node *root,int arr[],int *k,int level)
{
if(root==NULL)
return ;
if(level==0)
arr[(*k)++]=root->data ;
tree_to_array(root->left,arr,&(*k),level-1) ;
tree_to_array(root->right,arr,&(*k),level-1) ;
}
void array_to_tree(struct node *root,int arr[],int *k,int level)
{
if(root==NULL)
return ;
if(level==0)
root->data=arr[--(*k)] ;
array_to_tree(root->left,arr,&(*k),level-1) ;
array_to_tree(root->right,arr,&(*k),level-1) ;
}
---------------------------------------------------------------------------------
perduoWid-zoBaltimore Mark Mastracci https://marketplace.visualstudio.com/items?itemName=matacca-i.Descargar-Nope-Nope-Nurses-gratuita
ReplyDeleteelpiapenci
WvinganYconc_ge_Erie Alexis Alexander Autodesk Maya
ReplyDeleteExpress VPN
Disk Drill
burghandtesba
Wlode0cons-tsu Jordan Shamoon program
ReplyDeletethere
nalematme