Skip to content

Instantly share code, notes, and snippets.

@Baekjoon
Created October 24, 2015 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Baekjoon/a0fbbde7274d2c125fcd to your computer and use it in GitHub Desktop.
Save Baekjoon/a0fbbde7274d2c125fcd to your computer and use it in GitHub Desktop.
1991
import java.util.*;
public class Main {
public static void preorder(int[][] a, int x) {
if (x == -1) return;
System.out.print((char)(x+'A'));
preorder(a,a[x][0]);
preorder(a,a[x][1]);
}
public static void inorder(int[][] a, int x) {
if (x == -1) return;
inorder(a,a[x][0]);
System.out.print((char)(x+'A'));
inorder(a,a[x][1]);
}
public static void postorder(int[][] a, int x) {
if (x == -1) return;
postorder(a,a[x][0]);
postorder(a,a[x][1]);
System.out.print((char)(x+'A'));
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
int[][] a = new int[26][2];
for (int i=0; i<n; i++) {
String line = sc.nextLine();
int x = line.charAt(0) - 'A';
char y = line.charAt(2);
char z = line.charAt(4);
if (y == '.') {
a[x][0] = -1;
} else {
a[x][0] = y-'A';
}
if (z == '.') {
a[x][1] = -1;
} else {
a[x][1] = z-'A';
}
}
preorder(a,0);
System.out.println();
inorder(a,0);
System.out.println();
postorder(a,0);
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment