因为代码大部分是借鉴别人的,因此设为【转载】
第一题:03-树1 树的同构 (25 分)
给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。
图1
图2
现给定两棵树,请你判断它们是否是同构的。
输入格式:
输入给出2棵二叉树树的信息。对于每棵树,首先在一行中给出一个非负整数N (≤10),即该树的结点数(此时假设结点从0到N−1编号);随后N行,第i行对应编号第i个结点,给出该结点中存储的1个英文大写字母、其左孩子结点的编号、右孩子结点的编号。如果孩子结点为空,则在相应位置上给出“-”。给出的数据间用一个空格分隔。注意:题目保证每个结点中存储的字母是不同的。
输出格式:
如果两棵树是同构的,输出“Yes”,否则输出“No”。
输入样例1(对应图1):
8
A 1 2
B 3 4
C 5 -
D - -
E 6 -
G 7 -
F - -
H - -
8
G - 4
B 7 6
F - -
A 5 1
H - -
C 0 -
D - -
E 2 -
输出样例1:
Yes
输入样例2(对应图2):
8
B 5 7
F - -
A 0 3
C 6 -
H - -
D - -
G 4 -
E 1 -
8
D 6 -
B 5 -
E - -
H - -
C 0 2
G - 3
F - -
A 1 4
输出样例2:
No
#include<stdio.h>
#define MaxTree 11
#define ElementType char
#define Tree int
#define Null -1
struct TreeNode{
ElementType Element;
Tree Left;
Tree Right;
}T1[MaxTree],T2[MaxTree];
Tree BuildTree(struct TreeNode[]);
int Isomorphic(Tree R1,Tree R2);
int main(void){
Tree R1,R2;
R1=BuildTree(T1);
R2=BuildTree(T2);
if(Isomorphic(R1,R2))printf("Yes");
else printf("No");
return 0;
}
Tree BuildTree(struct TreeNode T[]){
Tree root=Null;
int i,N;
char cl,cr;
int check[MaxTree];
scanf("%d\n",&N);
if(N){
for(i=0;i<N;i++) check[i]=1;
for(i=0;i<N;i++){
scanf("%c %c %c\n",&T[i].Element,&cl,&cr);
if(cl!='-'){
T[i].Left=cl-'0';
check[T[i].Left]=0;
}
else T[i].Left=Null;
if(cr!='-'){
T[i].Right=cr-'0';
check[T[i].Right]=0;
}
else T[i].Right=Null;
}
for(i=0;i<N;i++)if(check[i])break;
root=i;
}
return root;
}
int Isomorphic(Tree R1,Tree R2){
if((R1==Null)&&(R2==Null))return 1;
if(((R1==Null)&&(R2!=Null))||((R1!=Null)&&(R2==Null)))return 0;
if(T1[R1].Element!=T2[R2].Element)return 0;
if((T1[R1].Left==Null)&&(T2[R2].Left==Null))return Isomorphic(T1[R1].Right,T2[R2].Right);
if(((T1[R1].Left!=Null)&&(T2[R2].Left!=Null))&&((T1[T1[R1].Left].Element)==(T2[T2[R2].Left].Element)))
return (Isomorphic(T1[R1].Left,T2[R2].Left)&&Isomorphic(T1[R1].Right,T2[R2].Right));
else return(Isomorphic(T1[R1].Left,T2[R2].Right)&&Isomorphic(T1[R1].Right,T2[R2].Left));
}
第二题:03-树2 List Leaves (25 分)
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.
Output Specification:
For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.
Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
4 1 5
参考链接:https://blog.csdn.net/luyaxige/article/details/79769286
#include <stdio.h>
#include <stdlib.h>
#define MaxTree 10
#define Null -1
#define Tree int
#define ElementType int //char
struct TreeNode{
Tree Left;
Tree Right;
}T[MaxTree];
struct Node{
ElementType Data;
struct Node *Next;
};
struct QNode{
struct Node *front;
struct Node *rear;
};
typedef struct QNode *Queue;
Tree BulidTree(struct TreeNode T[]);
Queue CreatQueue();
Queue AddQ(Queue Q,int x);
ElementType DeleteQ(Queue Q);
void Print(Tree Root);
int main(void){
Tree R;
R=BulidTree(T);
Print(R);
return 0;
}
Tree BulidTree(struct TreeNode T[]){
int Root=Null;
int i,N;
char cr,cl;
int check[MaxTree];
scanf("%d",&N);
if(N){
for(i=0;i<N;i++) check[i]=1;
for(i=0;i<N;i++){
scanf("\n%c %c",&cl,&cr);
if(cl!='-'){
T[i].Left=cl-'0';
check[T[i].Left]=0;
}
else T[i].Left=Null;
if(cr!='-'){
T[i].Right=cr-'0';
check[T[i].Right]=0;
}
else T[i].Right=Null;
}
for(i=0;i<N;i++) if(check[i])break;
Root=i;
}
return Root;
}
Queue CreatQueue(){
Queue Q;
Q=(Queue)malloc(sizeof(struct QNode));
Q->front=Q->rear=NULL;
return Q;
}
Queue AddQ(Queue Q,int x){
struct Node *temp;
temp=(struct Node*)malloc(sizeof(struct Node));
temp->Data=x;
temp->Next=NULL;
if(Q->front==NULL){
Q->front=temp;
Q->rear=temp;//temp;
}
else{
Q->rear->Next=temp;
Q->rear=temp;
}
return Q;
}
ElementType DeleteQ(Queue Q){
struct Node *FrontCell;
ElementType FrontElem;
if(Q->front==NULL){
return Null;//NULL;
}
FrontCell=Q->front;
if(Q->front==Q->rear) Q->front=Q->rear=NULL;
else
Q->front=Q->front->Next;
FrontElem=FrontCell->Data;
free(FrontCell);
return FrontElem;
}
void Print(Tree Root){
Queue Q;
Tree t;
int i=0;
int arr[MaxTree];
if(Root==Null)return;
Q=CreatQueue();
AddQ(Q,Root);
while(Q->front!=NULL){
t=DeleteQ(Q);
if(T[t].Left==Null && T[t].Right==Null)
arr[i++]=t;
if(T[t].Left!=Null)AddQ(Q,T[t].Left);
if(T[t].Right!=Null)AddQ(Q,T[t].Right);
}
printf("%d",arr[0]);
for(int j=1;j<i;j++)
printf(" %d",arr[j]);
}