#include
#include
using namespace std;
class node
{
public:
int data;
node *left;
node *right;
node (int x)
{
data = x;
left = NULL;
right = NULL;
}
};
int num = -1;
int height;
node *buildTreeLevelWise ()
{
int d;
cin >> d;
node* root = new node (d);
queue<node*> q;
q.push(root);
while (!q.empty ())
{
node *f = q.front ();
q.pop ();
int c1, c2;
cin >> c1 >> c2;
if (c1 != -1)
{
f->left = new node (c1);
q.push (f->left);
}
if (c2 != -1)
{
f->right = new node (c2);
q.push (f->right);
}
}
return root;
}
int treeheight (node * root)
{
if (root == NULL)
{
return 0;
}
int leftsub;
int rightsub;
leftsub = treeheight (root->left);
rightsub = treeheight (root->right);
return max (leftsub, rightsub) + 1;
}
void atklevel (node * root, int k)
{
if (root == NULL)
{
return;
}
if (k == 1)
{
cout << root->data << " ";
}
atklevel (root->left, k - 1);
atklevel (root->right, k - 1);
}
void printalllevel (node * root)
{
int h = treeheight (root);
for (int i = 1; i <= h; i++)
{
atklevel (root, i);
cout << endl;
}
}
void rightside (node * root)
{
if (root == NULL)
{
return;
}
if ((height - treeheight (root)) > num)
{
cout << root->data << " ";
num++;
}
rightside (root->right);
rightside (root->left);
}
int main ()
{
node *root = buildTreeLevelWise();
height = treeheight (root);
rightside (root);
return 0;
}