|
| 1 | +package Tree.P2263; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.StringTokenizer; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + |
| 8 | + static int n; |
| 9 | + static int[] in, post; |
| 10 | + static StringBuilder sb = new StringBuilder(); |
| 11 | + |
| 12 | + public static void main(String[] args) throws Exception { |
| 13 | + System.setIn(new FileInputStream("src/Tree/P2263/input.txt")); |
| 14 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 15 | + |
| 16 | + n = Integer.parseInt(br.readLine()); |
| 17 | + in = new int[n]; |
| 18 | + post = new int[n]; |
| 19 | + |
| 20 | + StringTokenizer st1 = new StringTokenizer(br.readLine()); |
| 21 | + StringTokenizer st2 = new StringTokenizer(br.readLine()); |
| 22 | + for (int i = 0; i < n; i++) { |
| 23 | + in[i] = Integer.parseInt(st1.nextToken()); |
| 24 | + post[i] = Integer.parseInt(st2.nextToken()); |
| 25 | + } |
| 26 | + |
| 27 | + search(0, n-1, 0, n-1); |
| 28 | + System.out.println(sb.toString()); |
| 29 | + } |
| 30 | + |
| 31 | + static void search(int i_s, int i_e, int p_s, int p_e) { |
| 32 | + int root = post[p_e], mid; |
| 33 | + sb.append(root).append(" "); |
| 34 | + if (i_s == i_e) return; |
| 35 | + for (mid = i_s; mid <= i_e; mid++) { |
| 36 | + if (in[mid] == root) break; |
| 37 | + } |
| 38 | + if (i_s <= mid-1) search(i_s, mid-1, p_s, p_s+(mid-i_s)-1); |
| 39 | + if (mid < i_e) search(mid+1, i_e, p_s+(mid-i_s), p_e-1); |
| 40 | + } |
| 41 | +} |
0 commit comments