#include<bits/stdc++.h>
using
namespace
std;
struct
node
{
struct
node* left;
struct
node* right;
int
data;
};
struct
node* newNode(
int
data)
{
struct
node* temp =
new
node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return
temp;
}
int
heightOfTree(
struct
node* root)
{
if
(root == NULL)
return
0;
int
lheight = heightOfTree(root->left);
int
rheight = heightOfTree(root->right);
return
max(lheight + 1 ,rheight + 1);
}
void
printRightToLeft(
struct
node* root ,
int
level)
{
if
(root == NULL)
return
;
if
(level == 1)
cout << root->data <<
" "
;
else
if
(level > 1)
{
printRightToLeft(root->right ,level - 1);
printRightToLeft(root->left ,level - 1);
}
}
void
printLeftToRight(
struct
node* root ,
int
level)
{
if
(root == NULL)
return
;
if
(level == 1)
cout << root->data <<
" "
;
else
if
(level > 1)
{
printLeftToRight(root->left ,level - 1);
printLeftToRight(root->right ,level - 1);
}
}
void
printZigZag(
struct
node* root )
{
int
flag = 0;
int
height = heightOfTree(root);
for
(
int
i = 1 ; i <= height ; i++)
{
if
(flag == 1)
{
printRightToLeft(root ,i);
flag = 0;
}
else
if
(flag == 0)
{
printLeftToRight(root ,i);
flag = 1;
}
}
}
int
main()
{
struct
node* root = newNode(7);
root->left = newNode(4);
root->right = newNode(5);
root->left->left = newNode(9);
root->right->right = newNode(10);
root->left->left->left = newNode(6);
root->left->left->right = newNode(11);
printZigZag(root);
return
0;
}