【C】二叉树的遍历

1020. Tree Traversals (25)

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

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2
即:后序+中序=层次bfs

#include<queue>
#include<stdio.h>
using namespace std;
struct node{
	int data;
	node* lchild;
	node* rchild;
};
int pre[32],in[32],post[32];
int n;
//[postl,postr],[inl,inr]
node* create(int postl,int postr,int inl,int inr){
	if(postl>postr) return NULL;
	node* root=new node;
	root->data=post[postr];
	int k;
	for(k=inl;k<=inr;k++){
		if(post[postr]==in[k]) break;
	}
	int numl=k-inl;
	root->lchild=create(postl,postl+numl-1,inl,k-1);
	root->rchild=create(postl+numl,postr-1,k+1,inr);
	return root;
}
int num=0;
void bfs(node* root){
	queue<node*> q;
	q.push(root);
	while(!q.empty()){
		node* p=q.front();
		q.pop();
		printf("%d",p->data);
		num++;
		if(num<n) printf(" ");
		if(p->lchild!=NULL) q.push(p->lchild);
		if(p->rchild!=NULL) q.push(p->rchild);
	}
}
int main(){
	scanf("%d",&n);
	int i;
	for(i=0;i<n;i++){
		scanf("%d",&post[i]);
	}
	for(i=0;i<n;i++){
		scanf("%d",&in[i]);
	}
	node* root=create(0,n-1,0,n-1);
	bfs(root);
	return 0;
}


1086. Tree Traversals Again (25)

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

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.


Figure 1

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.

Output Specification:

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:
6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop
Sample Output:
3 4 2 6 5 1
即:先序+中序=后序
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<string>
#include<stack>
using namespace std;
stack<int> s;
struct node{
	int data;
	node* lchild;
	node* rchild;
};
int pre[32],in[32],post[32];
int n,num=0;
node* create(int prel,int prer,int inl,int inr){
	if(prel>prer) return NULL;
	node* root=new node;
	root->data=pre[prel];
	int k;
	for(k=inl;k<=inr;k++){
		if(pre[prel]==in[k]) break;
	}
	int numl=k-inl;
	root->lchild=create(prel+1,prel+numl,inl,k-1);
	root->rchild=create(prel+numl+1,prer,k+1,inr);
	return root;
}
void visit(node* root){
	if(root->lchild) visit(root->lchild);
	if(root->rchild) visit(root->rchild);
	num++;
	if(num==n) printf("%d",root->data);
	else printf("%d ",root->data);
}
int deal(string str){
	if(str.length()==1) return (int)(str[0]-'0');
	else{
		int a=(int)(str[0]-'0');
		int b=(int)(str[1]-'0');
		return a*10+b;
	}
}
int main(){
	int i,j=0,k=0;
	scanf("%d",&n);
	getchar();
	for(i=0;i<2*n;i++){
		string str;
		getline(cin,str);
		if(str.length()>4){
			string strr=str.substr(5,str.length()-5);
			int a=deal(strr);
			pre[j++]=a;
			s.push(a);
		}
		else{
			int a=s.top();
			in[k++]=a;
			s.pop();
		}
	}
	node* root=create(0,j-1,0,k-1);
	visit(root);
	return 0;
}


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
即:打印出层序和中序
#include<stdio.h>
#include<queue>
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
struct node{
	int data;
	node* lchild;
	node* rchild;
};
int leftt[11],rightt[11];
int n,num1=0,num2=0;
void initial(){
	int i;
	for(i=0;i<11;i++){
		leftt[i]=-1;
		rightt[i]=-1;
	}
}
void deal(string str,int i){
	char a=str[0];
	char b=str[2];
	if(a>='0'&&a<='9'){//n<=10,编号最大是9
		rightt[i]=(int)(a-'0');
	}
	else rightt[i]=-1;
	if(b>='0'&&b<='9'){//n<=10,编号最大是9
		leftt[i]=(int)(b-'0');
	}
	else leftt[i]=-1;
}
node* create(int index){
	node* root=new node;
	root->data=index;
	if(leftt[index]!=-1) root->lchild=create(leftt[index]);
	else root->lchild=NULL;
	if(rightt[index]!=-1) root->rchild=create(rightt[index]);
	else root->rchild=NULL;
	return root;
}
void bfs(node* root){
	queue<node*> q;
	q.push(root);
	while(!q.empty()){
		node* p=q.front();
		q.pop();
		num1++;
		if(num1<n) printf("%d ",p->data);
		else printf("%d",p->data);
		if(p->lchild!=NULL) q.push(p->lchild);
		if(p->rchild!=NULL) q.push(p->rchild);
	}
}
void visitin(node* root){
	node* p=root;
	if(p->lchild!=NULL) visitin(p->lchild);
	num2++;
	if(num2<n) printf("%d ",p->data);
	else printf("%d",p->data);
	if(p->rchild!=NULL) visitin(p->rchild);
}
int main(){
	int i;
	scanf("%d",&n);
	getchar();
	initial();
	for(i=0;i<n;i++){
		string str;
		getline(cin,str);
		deal(str,i);
	}
	int chuxian[12]={0};
	for(i=0;i<n;i++){//下标要加一是因为上文有leftt、rightt=-1
		chuxian[leftt[i]+1]++;
		chuxian[rightt[i]+1]++;
	}//统计哪个节点不是别人的孩子
	for(i=0;i<n;i++){
		if(chuxian[i]==0) break;
	}
	node* root=create(i-1);
	bfs(root);
	printf("\n");
	visitin(root);
	return 0;
}
注:由于使用了using namespace std;代码中不应有left、right字样,否则会无法识别。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值