本文主要是介绍题目1521:二叉树的镜像,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- 题目描述:
-
输入一个二叉树,输出其镜像。
- 输入:
-
输入可能包含多个测试样例,输入以EOF结束。
对于每个测试案例,输入的第一行为一个整数n(0<=n<=1000,n代表将要输入的二叉树节点的个数(节点从1开始编号)。接下来一行有n个数字,代表第i个二叉树节点的元素的值。接下来有n行,每行有一个字母Ci。
Ci=’d’表示第i个节点有两子孩子,紧接着是左孩子编号和右孩子编号。
Ci=’l’表示第i个节点有一个左孩子,紧接着是左孩子的编号。
Ci=’r’表示第i个节点有一个右孩子,紧接着是右孩子的编号。
Ci=’z’表示第i个节点没有子孩子。
- 输出:
-
对应每个测试案例,
按照前序输出其孩子节点的元素值。
若为空输出NULL。
代码一:在构建树的过程中直接对左右孩子进行交换。
#include<stdio.h>
#include<stdlib.h>typedef struct tree
{int data;struct tree *left;struct tree *right;
}Node;
Node *Atree[1099];void create(Node *node[],int a[], int n)
{char type;int left,right;for(int i = 1; i <= n; i++){node[i]->data = a[i];scanf("\n%c",&type);if(type == 'd'){scanf("%d %d",&left,&right);node[i]->right = node[left];node[i]->left = node[right];}else if(type == 'l'){scanf("%d",&left);node[i]->right = node[left];node[i]->left = NULL; }else if(type == 'r'){scanf("%d",&right);node[i]->right = NULL;node[i]->left = node[right];}else if(type == 'z'){node[i]->left = NULL;node[i]->right = NULL;}}
}//前序打印树
int flag = 0;
void print(Node *tree)
{if(tree != NULL){if(flag == 0){printf("%d",tree->data);flag = 1;}elseprintf(" %d",tree->data);print(tree->left);print(tree->right);}
}
int main()
{int n;while(scanf("%d",&n) != EOF){if( n <= 0){printf("NULL\n");}else{int a[n + 1];for(int i = 1; i <= n; i++)Atree[i] = new Node;for(int i = 1; i <= n; i++)scanf("%d",&a[i]);create(Atree,a,n);print(Atree[1]);printf("\n");//清空for(int i = 1;i <= n;i++){delete Atree[i];Atree[i]=NULL;}}}
}
代码二:构建树后按照父节点->右孩子->左孩子的顺序打印
#include<stdio.h>
#include<stdlib.h>typedef struct tree
{int data;struct tree *left;struct tree *right;
}Node;
Node *Atree[1099];void create(Node *node[],int a[], int n)
{char type;int left,right;for(int i = 1; i <= n; i++){node[i]->data = a[i];scanf("\n%c",&type);if(type == 'd'){scanf("%d %d",&left,&right);node[i]->left = node[left];node[i]->right = node[right];}else if(type == 'l'){scanf("%d",&left);node[i]->left = node[left];node[i]->right = NULL; }else if(type == 'r'){scanf("%d",&right);node[i]->left = NULL;node[i]->right = node[right];}else if(type == 'z'){node[i]->left = NULL;node[i]->right = NULL;}}
}//按父节点->右孩子->左孩子的顺序打印
int flag = 0;
void print(Node *tree)
{if(tree != NULL){if(flag == 0){printf("%d",tree->data);flag = 1;}elseprintf(" %d",tree->data);print(tree->right);print(tree->left);}
} int main()
{int n;while(scanf("%d",&n) != EOF){if( n <= 0){printf("NULL\n");}else{int a[n + 1];for(int i = 1; i <= n; i++)Atree[i] = new Node;for(int i = 1; i <= n; i++)scanf("%d",&a[i]);create(Atree,a,n);print(Atree[1]);printf("\n");//清空for(int i = 1;i <= n;i++){delete Atree[i];Atree[i]=NULL;}}}
}
代码三:构建树后递归交换树的左右子节点,最后前序打印。
#include<stdio.h>
#include<stdlib.h>typedef struct tree
{int data;struct tree *left;struct tree *right;
}Node;
Node *Atree[1099];void create(Node *node[],int a[], int n)
{char type;int left,right;for(int i = 1; i <= n; i++){node[i]->data = a[i];scanf("\n%c",&type);if(type == 'd'){scanf("%d %d",&left,&right);node[i]->left = node[left];node[i]->right = node[right];}else if(type == 'l'){scanf("%d",&left);node[i]->left = node[left];node[i]->right = NULL; }else if(type == 'r'){scanf("%d",&right);node[i]->left = NULL;node[i]->right = node[right];}else if(type == 'z'){node[i]->left = NULL;node[i]->right = NULL;}}
}void mirrorTree(Node *tree)
{if(tree == NULL || (tree->left == NULL && tree->right == NULL))return;Node *p = tree->left;tree->left = tree->right;tree->right = p;mirrorTree(tree->left);mirrorTree(tree->right);
}//前序打印树
int flag = 0;
void print(Node *tree)
{if(tree != NULL){if(flag == 0){printf("%d",tree->data);flag = 1;}elseprintf(" %d",tree->data);print(tree->left);print(tree->right);}
}
int main()
{int n;while(scanf("%d",&n) != EOF){if( n <= 0){printf("NULL\n");}else{int a[n + 1];for(int i = 1; i <= n; i++)Atree[i] = new Node;for(int i = 1; i <= n; i++)scanf("%d",&a[i]);create(Atree,a,n);mirrorTree(Atree[1]);print(Atree[1]);printf("\n");//清空for(int i = 1;i <= n;i++){delete Atree[i];Atree[i]=NULL;}}}
}
这篇关于题目1521:二叉树的镜像的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!