import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class boj1991 {
public static class Tree {
Node root = new Node("A");
public void insertNode(Node root, String data, String leftData, String rightData) {
if (root == null) return;
if (root.data.equals(data)) {
if (!leftData.equals(".")) {
root.left = new Node(leftData);
}
if (!rightData.equals(".")) {
root.right = new Node(rightData);
}
} else {
insertNode(root.left, data, leftData, rightData);
insertNode(root.right, data, leftData, rightData);
}
}
public void preOrder(Node node) {
if (node == null) return;
System.out.print(node.data);
preOrder(node.left);
preOrder(node.right);
}
public void inOrder(Node node) {
if (node == null) return;
inOrder(node.left);
System.out.print(node.data);
inOrder(node.right);
}
public void postOrder(Node node) {
if (node == null) return;
postOrder(node.left);
postOrder(node.right);
System.out.print(node.data);
}
}
public static class Node {
public String data;
public Node left;
public Node right;
public Node(String data) {
this.data = data;
}
public Node(String data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
Tree tree = new Tree();
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
String data = st.nextToken();
String leftData = st.nextToken();
String rightData = st.nextToken();
tree.insertNode(tree.root, data, leftData, rightData);
}
tree.preOrder(tree.root);
System.out.println();
tree.inOrder(tree.root);
System.out.println();
tree.postOrder(tree.root);
}
}
Leave a comment