//自己重新写了一个Java的,按Ctrl-z可以结束,基本思路是用位图来实现快速发现根节点。然后
//多叉树的存储为链表结构
import java.io.*;
import java.util.*;
class Node {
int val;
Node child;
Node next;
Node(int v) {
val = v;
child = null;
next = null;
}
}
public class Build {
public static void myTraverse(Node root){
Node p = root;
//hierarchy traverse
while(p != null){
//cur root
System.out.print(p.val + " ");
//siblings
while(p.next != null){
System.out.print(p.next.val + " ");
p = p.next;
}
//child
if(p.child != null){
p = p.child;
}
else{
break;
}
}
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
//bitmap
//save every node
//find element quickly
Node[] arr = new Node[101];
for (int i = 0; i < 101; i++) {
arr[i] = null;
}
Node coreRoot = null;
int count = 0;
//Ctrl-z end input
while (in.hasNextLine()) {
String line = in.nextLine();
String strs[] = line.split("\\s+");
Node root = null;
for (int i = 0; i < strs.length; i++) {
int num = Integer.valueOf(strs[i]);
//cur root
if (i == 0) {
if (arr[num] == null) {
Node temp = new Node(num);
arr[num] = temp;
}
root = arr[num];
}
//this level siblings
else {
Node temp = null;
if (arr[num] == null) {
temp = new Node(num);
arr[num] = temp;
}
temp = arr[num];
Node p = root;
while (p.next != null) {
p = p.next;
}
p.next = temp;
}
}
//core root
if (count == 0) {
coreRoot = root;
}
count += 1;
}
myTraverse(coreRoot);
}
}