//04-树4 是否同一棵二叉搜索树
import java.util.ArrayList;
import java.util.Scanner;
class TNode{
int data = 0;
int right = -1;
int left = -1;
}
public class Main {
static TNode[] add(TNode[] Ts, TNode t, TNode T){
if(t.data < T.data){
if(T.left != -1){
add(Ts, t, Ts[T.left - 1]);
}
else{
T.left = t.data;
}
}
else{
if(T.right != -1){
add(Ts, t, Ts[T.right - 1]);
}
else{
T.right = t.data;
}
}
return Ts;
}
static TNode[] readT(Scanner s, int N){
TNode[] Ts = new TNode[N];
int p = 0;
int fir = 0;
fir = s.nextInt();
Ts[fir - 1] = new TNode();
Ts[fir - 1].data = fir;
for(int i = 1; i < N; i ++){
p = s.nextInt();
Ts[p - 1] = new TNode();
Ts[p - 1].data = p;
Ts = add(Ts, Ts[p - 1], Ts[fir - 1]);
}
return Ts;
}
static String judge(TNode[] T1, TNode[] T2){
String result = "Yes";
for(int i = 0; i < T1.length; i ++){
if(T1[i].data != T2[i].data || T1[i].left != T2[i].left || T1[i].right != T2[i].right){
result = "No";
break;
}
}
return result;
}
static void test(Scanner s){
int N = s.nextInt();
if(N != 0){
int L = s.nextInt();
TNode[] T1;
TNode[] T2;
T1 = readT(s, N);
for(int i = 0; i < L; i ++){
T2 = readT(s, N);
System.out.println(judge(T1, T2));
}
test(s);
}
else{
System.exit(0);
}
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
test(s);
}
}
04-树4 是否同一棵二叉搜索树
最新推荐文章于 2024-07-13 13:55:40 发布