public TreeNode buildTreeRec(int[] inorder, int[] postorder, int s1, int e1, int s2, int e2) {
        if(e1 == s1 && s2 == e2) return new TreeNode(inorder[s1]);

        if(postorder[e2] == inorder[s1]){
            TreeNode root = new TreeNode(postorder[e2]);
            TreeNode right = buildTreeRec(inorder, postorder, s1+1, e1, e2-1-(e1-(s1+1)), e2-1);
            root.right = right;
            return root;
        }

        if(postorder[e2] == inorder[e1]){
            TreeNode root = new TreeNode(postorder[e2]);
            TreeNode left = buildTreeRec(inorder, postorder, s1, e1-1, s2, e2-1);
            root.left = left;
            return root;
        }

        int i = s1;
        for(; i <= e1; ++i){
            if(inorder[i] == postorder[e2]) break;
        }

        TreeNode root = new TreeNode(postorder[e2]);
        TreeNode left = buildTreeRec(inorder, postorder, s1, i-1, s2, s2+(i-1-s1));
        root.left = left;

        TreeNode right = buildTreeRec(inorder, postorder, i+1, e1, e2-1-(e1-(i+1)), e2-1);
        root.right = right;
        return root;
    }