PAT 甲级 A1102 Invert a Binary Tree

The following is from Max Howell @twitter:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off.

Now it's your turn to prove that YOU CAN invert a binary tree!

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 from 0 to N−1, 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 the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. 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:

3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1

题意:从0开始到n,依次给出结点的左右孩子,确定一颗二叉树,然后反转该二叉树,输出该反转二叉树的层序遍历序列和中序遍历序列。

思路:用后序遍历中对根结点操作时,将左右孩子的地址交换,即可做到整树反转,先反转,然后再层次遍历和中序遍历就好了,像这种直接给出孩子的地址(编号)的,用静态二叉树会好做一些;

代码:
 

#include<iostream>
#include<queue>
using namespace std;
const int maxn = 50;
int n,num=0;
char lchild,rchild;
bool notroot[maxn]={false};
struct node{//线性输入,按编号顺序存储结点,用静态二叉树会好做一点; 
	int lchild;
	int rchild;
}Node[maxn];

int deal(char c){
	if(c=='-')return -1;//没有子结点;
	else {
		notroot[c-'0']=true;//是别人的孩子就不是根节点,所以定为true,为下面找根节点作准备:
		return c-'0'; 
	} 
}

int findroot( ){//找到根节点,因为遍历树是需要根节点的,所以要找出根节点; 
	for(int i=0;i<n;i++){
		if(notroot[i]==false){
			return i;
		}
	}
}
void postorder(int root){//实现二叉树反转; 
	if(root==-1)return ;
	postorder(Node[root].lchild);
	postorder(Node[root].rchild);
	swap(Node[root].lchild,Node[root].rchild);
}

void print(int id){
	printf("%d",id);
	num++;
	if(num<n)printf(" ");
	else printf("\n");
}

void BFS(int root){
	queue<int > q;//还是存放着地址,因为还是通过下标(地址)来访问结点;
	q.push(root);
	while(!q.empty()){
		int top=q.front();
		q.pop();
		print(top); 
		if(Node[top].lchild!=-1) q.push(Node[top].lchild);
		if(Node[top].rchild!=-1) q.push(Node[top].rchild);
	} 
}

void inorder(int root){
	//if(Node[root].lchild==-1)return ;
	//if(Node[root].rchild==-1)return ;
	//递归边界是root==-1,即当前指针指向NULL,而不是左右孩子指向NULL,如果是左右孩子指向NULL,返回就是到上叶子结点的上一层了,导致叶子结点没有输出; 
	if(root==-1)return ;
	inorder(Node[root].lchild);
	print(root);
	inorder(Node[root].rchild);
}
 
int main( ){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%*c%c %c",&lchild,&rchild);//输入i结点的左右子树的编号;
		Node[i].lchild=deal(lchild);//因为要让字符转换成数字编号,且要做一些相关操作,特自定义函数deal; 
		Node[i].rchild=deal(rchild);
	}//建立好树;
	int root=findroot( ); 
	postorder(root);//反转二叉树; 
	
	BFS(root);//层序遍历序列:
	num=0;
	inorder(root); 
	return 0;	
} 

#include<iostream>
#include<queue>
using namespace std;
const int Maxn = 10010;
struct node {
	int lchild;
	int rchild;
}node[Maxn];
char lchild, rchild;
int n, num = 0;
bool noRoot[Maxn] = {false};

int strToNum(int child) {
	if(child == '-')
		return -1;
	else {
		int m = child - '0';
		noRoot[m] = true;
		return m;	
	}
}

int findRoot() {
	for(int i = 0; i < n; i++)
		if(noRoot[i] == false)
			return i;
}

void print(int root) {
	cout << root;
	if(++num < n)
		cout << " ";
	else 
		cout << "\n";
}

void postorder(int root) {
	if(root == -1)
		return;
	postorder(node[root].lchild);
	postorder(node[root].rchild);
	swap(node[root].lchild, node[root].rchild);
}

void inorder(int root) {
	if(root == -1)
		return ;
	inorder(node[root].lchild);
	print(root);
	inorder(node[root].rchild);
}

void layerorder(int root) {
	queue<int> q;
	q.push(root);
	while(!q.empty()) {
		int top = q.front();
		q.pop();
		print(top);
		if(node[top].lchild != -1) q.push(node[top].lchild);
		if(node[top].rchild != -1) q.push(node[top].rchild);
	}
}

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) {
		scanf("%*c%c %c", &lchild, &rchild);
		node[i].lchild = strToNum(lchild);
		node[i].rchild = strToNum(rchild);
	}
	int root = findRoot();
	postorder(root);
	layerorder(root);
	num = 0;
	inorder(root);
	return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值