PAT甲级真题及训练集(21)--1102. Invert a Binary Tree (25)

1102. Invert a Binary Tree (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代

/**
作者:一叶扁舟
时间:19:43 2017/7/7
思路:
	交换左右子树,并且层序输出和中序输出
例子:

*/
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;
#define SIZE 101

typedef struct TreeNode{
	int data;
	int leftChild;
	int rightChild;
}TreeNode;

int root[SIZE] = { 0 };//默认都是根节点
TreeNode treeNode[SIZE];//全局变量,存储树
int midNum = 0;//记录中序遍历输出的个数
int N;//总共的结点个数

//根据输入的字符,获取对应父节点的编号
int getNodeNum(char ch){
	if (ch == '-'){
		return -1;//没有该节点
	}
	else{
		int temp = ch - '0';
		root[temp] = 1;//temp值出现了,就说明该节点不是根节点,
		              //因为根节点是不可能有父节点的
		return temp;
	}

}
//找到根节点
int  getRoot(int *root,int N){
	for (int i = 0; i < N; i++){
		if (root[i] != 1){//根节点默认为0了
			return i;
		}
	
	}
	return -1;
}
//层次遍历
void  levelOrder(int root,int N){
	queue<int> q;
	q.push(root);
	int num = 0;
	while (!q.empty()){
		int nodeNum  = q.front();
		q.pop();
		num++;
		if (treeNode[nodeNum].leftChild != -1){
			q.push(treeNode[nodeNum].leftChild);
		}

		if (treeNode[nodeNum].rightChild != -1){
			q.push(treeNode[nodeNum].rightChild);
		}

		printf("%d", treeNode[nodeNum].data);
		if (num != N ){
			printf(" ");
		}	
	}

}

//中序遍历
void middleOrder( int root){
	if (root != -1){
		middleOrder(treeNode[root].leftChild);
		printf("%d", treeNode[root].data);
		midNum++;
		if (midNum != N ){
			printf(" ");
		}
		middleOrder( treeNode[root].rightChild);	
	}
	return;

}

void exchangeNode( int rootNum){
	if (rootNum == -1){
		return;
	}
	int left = treeNode[rootNum].leftChild;
	int right = treeNode[rootNum].rightChild;
	treeNode[rootNum].leftChild = right;
	treeNode[rootNum].rightChild = left;
	exchangeNode(left);
	exchangeNode(right);
}
int main(){
	scanf("%d", &N);
	for (int i = 0; i < N; i++){
		char ch1, ch2;
		//%*c吸收掉换行符
		scanf("%*c%c %c", &ch1, &ch2);
		int num1 = getNodeNum(ch1);
		int num2 = getNodeNum(ch2);
		treeNode[i].data = i;
		treeNode[i].leftChild = num1;
		treeNode[i].rightChild = num2;
	}
	//找到根节点
    int rootNum = getRoot(root, N);
	//交换左右子树
	exchangeNode( rootNum);
	//层次遍历
	levelOrder(rootNum, N);
	printf("\n");
	//中序遍历
	middleOrder( rootNum);
	printf("\n");
	system("pause");
	return 0;

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值