181 通过先序和中序数组生成后序数组
Table of Contents

ac

import org.omg.CORBA.INTERNAL;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

class TreeNode{
    TreeNode left;
    TreeNode right;
    int val;

    public TreeNode(int val) {
        this.val = val;
        left = null;
        right = null;
    }
}

public class Main {
    public static Scanner sc = new Scanner(System.in);

    public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    public static long mod = (long) Math.pow(2, 29);

    public static void main(String[] args) throws Exception {
        int n = sc.nextInt();
        int[] pre = new int[n];
        int[] in = new int[n];
        for(int i=0;i<n;i++){
            pre[i] = sc.nextInt();
        }
        for(int i=0;i<n;i++){
            in[i] = sc.nextInt();
        }

        TreeNode root = buildTree(pre,0,n-1,in,0,n-1);
        postOrder(root);
        String s = sb.toString();
        s = s.substring(0, s.length() - 1);
        System.out.println(s);
    }

    static TreeNode buildTree(int[] pre, int preL, int preR, int[] in, int inL, int inR){
        if(preL > preR){
            return null;
        }
        TreeNode root = new TreeNode(pre[preL]);
        int p = inL;
        while (p <= inR && in[p] != root.val){
            p ++;
        }
        root.left = buildTree(pre, preL+1, preL + p-inL, in, inL, p-1);
        root.right = buildTree(pre, preL + p-inL + 1, preR, in, p+1, inR);
        return root;
    }

    static StringBuilder sb = new StringBuilder();

    static void postOrder(TreeNode root){
        if(root == null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        sb.append(root.val + " ");
    }
}
/*
3
1 2 3
2 1 3

2 3 1
 */