import
java.util.Stack;
import
java.util.HashMap;
public
class
PrintPre {
static
int
postIndex;
void
fillPre(
int
[] in,
int
[] post,
int
inStrt,
int
inEnd,
Stack<Integer> s, HashMap<Integer, Integer> hm)
{
if
(inStrt > inEnd)
return
;
int
val = post[postIndex];
int
inIndex = hm.get(val);
postIndex--;
fillPre(in, post, inIndex +
1
, inEnd, s, hm);
fillPre(in, post, inStrt, inIndex -
1
, s, hm);
s.push(val);
}
void
printPreMain(
int
[] in,
int
[] post)
{
int
len = in.length;
postIndex = len -
1
;
Stack<Integer> s =
new
Stack<Integer>();
HashMap<Integer, Integer> hm =
new
HashMap<Integer, Integer>();
for
(
int
i =
0
; i < in.length; i++)
hm.put(in[i], i);
fillPre(in, post,
0
, len -
1
, s, hm);
while
(s.empty() ==
false
)
System.out.print(s.pop() +
" "
);
}
public
static
void
main(String ars[])
{
int
in[] = {
4
,
10
,
12
,
15
,
18
,
22
,
24
,
25
,
31
,
35
,
44
,
50
,
66
,
70
,
90
};
int
post[] = {
4
,
12
,
10
,
18
,
24
,
22
,
15
,
31
,
44
,
35
,
66
,
90
,
70
,
50
,
25
};
PrintPre tree =
new
PrintPre();
tree.printPreMain(in, post);
}
}