1102 Invert a Binary Tree (binary construct, the sequence and order output binary inversion layer)

1102 Invert a Binary Tree (25 分)

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

Ideas:

            1. The inverted output of the binary tree in order: that is, a non-inverted output of the binary tree traversal by the right-left mode (corresponding to the order tree traversal has been inverted)

           2. As for the output sequence recursively binary output, adding a subscript index only index at this time is the right node indexes 2 * n + 1 is the left node 2 * n + 2

Then the index in ascending order is the sequence traversal; (if still subscripting left node is 2 * n + 1, the right node subscript index 2 * n + 2 for sorting when the first through the height from low to high index of each layer is then sorted in descending order by the output node is then traversed sequence)

 

           3. For the premise is to build a binary tree traversal and find the root of the tree;

              Construction of a isRoot bool array type determines whether the root of the tree, and if so, its root index id is the tree, based on this determination node of the root node is not a child of any node;

         

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct Node{
	int id, index, l, r;       //存储树的结点id(排序后下标发生改变),索引位置(方便层序遍历),左结点Id,//右结点id
}node[10];
int n;
bool isRoot[10];                   //判断是否是根结点
vector<Node> in, level;
void inOrder(int root, int index){ //反向中序遍历 此时右结点的下标未index * 2 + 1;
	if(root == -1) return;
	node[root].index = index;
	inOrder(node[root].r, index * 2 + 1);
	in.push_back(node[root]);
	inOrder(node[root].l, index * 2 + 2); 
}
bool cmp(Node a, Node b){
	return a.index < b.index; //根据索引从小到达排序
}
int main(){
	cin >> n;
	string l, r;
	int root = 0;
	fill(isRoot, isRoot + 10, true);
	for(int i = 0; i < n; i++){
		cin >> l >> r;
		node[i].id = i;
		if(l == "-"){
			node[i].l = -1;
		}else{
			int  left = stoi(l);
			node[i].l = left;
			isRoot[left] = false;
		}
		if(r == "-"){
			node[i].r = -1; //devc ==是不报错的 
		}else{
			int right = stoi(r);
			node[i].r = right;
			isRoot[right] = false;
		}
	}
	while(isRoot[root] == false){
		root++;
	}
	
	inOrder(root, 0);
	level = in;
	sort(level.begin(), level.end(), cmp);
	for(int i = 0; i < level.size(); i++){
		if(i != 0)
			printf(" ");
		printf("%d", level[i].id);
	}
	printf("\n");
	for(int i = 0; i < in.size(); i++){
		if(i != 0)
			printf(" ");
		printf("%d", in[i].id);
	}
	return 0;
}

The second way: the index does not change the position of the left and right of the tree the tree, and then comparing the first height from small to large relatively the same height index (descending)

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct Node{
	int id, index, l, r, height;
}node[10];
int n;
bool isRoot[10];
vector<Node> in, level;
void inOrder(int root, int index, int height){
	if(root == -1) return;
	node[root].index = index;
	node[root].height = height;
	inOrder(node[root].r, index * 2 + 2, height + 1);
	in.push_back(node[root]);
	inOrder(node[root].l, index * 2 + 1, height + 1); 
}
bool cmp(Node a, Node b){
	if(a.height != b.height) 
		return a.height < b.height;
	else
		return  a.index > b.index;
}
int main(){
	cin >> n;
	string l, r;
	int root = 0;
	fill(isRoot, isRoot + 10, true);
	for(int i = 0; i < n; i++){
		cin >> l >> r;
		node[i].id = i;
		if(l == "-"){
			node[i].l = -1;
		}else{
			int  left = stoi(l);
			node[i].l = left;
			isRoot[left] = false;
		}
		if(r == "-"){
			node[i].r = -1; //devc ==是不报错的 
		}else{
			int right = stoi(r);
			node[i].r = right;
			isRoot[right] = false;
		}
	}
	while(isRoot[root] == false){
		root++;
	}
	
	inOrder(root, 0, 0);
	level = in;
	sort(level.begin(), level.end(), cmp);
	for(int i = 0; i < level.size(); i++){
		if(i != 0)
			printf(" ");
		printf("%d", level[i].id);
	}
	printf("\n");
	for(int i = 0; i < in.size(); i++){
		if(i != 0)
			printf(" ");
		printf("%d", in[i].id);
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_41698081/article/details/91351202