#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
class ListNode{
public:
ListNode(int k):val(k){
left = NULL;
right = NULL;
father = NULL;
}
ListNode* left;
ListNode* right;
ListNode* father;
int val;
};
void back(ListNode* root)
{
if (!root)
return;
ListNode* last = NULL;
ListNode* now = root;
while (now)
{
if (last == now->left)
{
if (now->right)
{
now = now->right;
}
else
{
cout<<now->val<<" ";
last = now;
now = now->father;
continue;
}
}
else if (last == now->right)
{
cout<<now->val<<" ";
last = now;
now = now->father;
continue;
}
while (now->left)
now = now->left;
cout<<now->val<<" ";
last = now;
now = now->father;
}
}
int main(int argc, char *argv[])
{
ListNode a1(1);
ListNode a2(2);
ListNode a3(3);
ListNode a4(4);
ListNode a5(5);
ListNode a6(6);
ListNode a7(7);
a1.left = &a2;
a1.right = &a3;
a2.father = &a1;
a3.father = &a1;
a2.left = &a4;
a2.right = &a5;
a3.left = &a6;
a3.right = &a7;
a4.father = &a2;
a5.father = &a2;
a6.father = &a3;
a7.father = &a3;
back(&a1);
return 0;
}