PAT甲级—— 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

 思路:这道题要求一棵反转二叉树的层序和中序序列,这里不需要真的去建立一棵反转的二叉树,只需要在遍历的时候交换左右子树的顺序就可以了。首先是对输入数据的处理,这里都是单个字符,可以用scanf %c的形式来读取,但是千万注意,在用scanf读取字符的时候是可以读入空格和换行符的,那么就需要用getchar来清除多余的空格和换行符。在建立二叉树的时候,题目给出了结点的编号,这里可以直接用静态的写法来构建二叉树,不需要建立指针。

代码:

#include <iostream>
#include<queue>
using namespace std;
const int maxn = 15;
int n;
struct node {
	int num;
	int lchild;
	int rchild;
}Node[maxn];

bool find_root[maxn] = { false };

int num1 = 0;
void invert_layer(int root)//层次遍历通用模板,这里将左右子树的位置调换了一下
{
	
	queue<node>q;
	q.push(Node[root]);
	while (!q.empty())
	{
		node now = q.front();
		printf("%d", now.num);
		num1++;
		if (num1 < n) printf(" ");
		q.pop();
		if (now.rchild != -1) q.push(Node[now.rchild]);
		if (now.lchild != -1) q.push(Node[now.lchild]);
	}

}

int num2 = 0;
void invert_in(int root)//中序遍历通用模板,这里将左右子树的位置调换了一下
{
	if (root == -1) return;
	invert_in(Node[root].rchild);
	printf("%d", Node[root].num);
	num2++;
	if (num2 < n) printf(" ");
	invert_in(Node[root].lchild);
}

int main()
{
	scanf("%d", &n); 
	getchar();//清除第一行结尾的换行符
	char x;
	for (int i = 0; i < n; i++)
	{
		Node[i].num = i;
		scanf("%c", &x);
		if (x == '-') Node[i].lchild = -1;
		else {
			Node[i].lchild = x - '0';
			find_root[Node[i].lchild] = true;
		}
		getchar();
		scanf("%c", &x);
		if (x == '-') Node[i].rchild = -1;
		else {
			Node[i].rchild = x - '0';
			find_root[Node[i].rchild] = true;
		}
		getchar();
	}
//	for (int i = 0; i < n; i++)
//	{
//		printf_s("%d %d %d\n", Node[i].num, Node[i].lchild, Node[i].rchild);
//	}

	int root;
	for (root = 0; root < n; root++)
	{
		if (find_root[root] != true) break;
	}
	//printf_s("%d", root);
	invert_layer(root);
	printf("\n");
	invert_in(root);
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值