您的位置:首页 > 其它

二叉树

2016-06-30 10:30 127 查看
根据后续遍历和中序遍历建树:


树的遍历

时间限制

400 ms

内存限制

65536 kB

代码长度限制

8000 B

判题程序

Standard

作者

陈越

给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。

输入格式:

输入第一行给出一个正整数N(<=30),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。

输出格式:

在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。
输入样例:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

输出样例:
4 1 6 3 5 7 2


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
int hou[35], zhong[35];
int num;
struct node {
int n;
struct node *left;
struct node *right;
};
struct node *creatNode(int n) {
struct node *tmp = (struct node *) malloc(sizeof(struct node));
tmp->n = n;
tmp->left = tmp->right = NULL;
return tmp;
};
struct node *creatTree(int l, int r, int ll, int rr) { //ll和rr表示中根遍历的下标,l和r表示后续遍历的下标
struct node *root = NULL;
int pos;
if(ll <= rr) {
for(int i = ll; i <= rr; i++) {
if(zhong[i] == hou[r]) {
pos = i;
break;
}
}
root = creatNode(hou[r]);
}
if(root) {
root->left = creatTree(l, l+pos-ll-1, ll, pos-1);
root->right = creatTree(l+pos-ll, r-1, pos+1, rr);
}
return root;
};
void leavel(struct node *root) {
if(!root) return ;
printf("%d", root->n);
queue<struct node *> q;
q.push(root);
while(!q.empty()) {
struct node *t = q.front();
q.pop();
if(t) {
if(t->left) {
q.push(t->left);
printf(" %d", t->left->n);
}
if(t->right) {
q.push(t->right);
printf(" %d", t->right->n);
}
}
}
}
int main() {
scanf("%d", &num);
for(int i = 0; i < num; i++) {
scanf("%d", &hou[i]);
}
for(int i = 0; i < num; i++) {
scanf("%d", &zhong[i]);
}
struct node * root = creatTree(0, num-1, 0, num-1);
leavel(root);
return 0;
}


根据前序遍历序列和中序遍历序列建树:


 玩转二叉树

时间限制

400 ms

内存限制

65536 kB

代码长度限制

8000 B

判题程序

Standard

作者

陈越

给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左右孩子对换。这里假设键值都是互不相等的正整数。

输入格式:

输入第一行给出一个正整数N(<=30),是二叉树中结点的个数。第二行给出其中序遍历序列。第三行给出其前序遍历序列。数字间以空格分隔。

输出格式:

在一行中输出该树反转后的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。
输入样例:
7
1 2 3 4 5 6 7
4 1 3 2 6 5 7

输出样例:
4 6 1 7 5 3 2


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
int qian[35], zhong[35];
int num;
struct node {
int n;
struct node *left;
struct node *right;
};
struct node *creatNode(int n) {
struct node *tmp = (struct node *) malloc(sizeof(struct node));
tmp->n = n;
tmp->left = tmp->right = NULL;
return tmp;
};
struct node *creatTree(int l, int r, int ll, int rr) {//ll和rr代表后序遍历的序号,l和r代表前序遍历的序号
struct node *root = NULL;
int pos;
if(ll <= rr) {
for(int i = ll; i <= rr; i++) {
if(zhong[i] == qian[l]) {
pos = i;
break;
}
}
root = creatNode(qian[l]);
}
if(root) {
root->left = creatTree(l+1, l+pos-ll, ll, pos-1);
root->right = creatTree(l+pos-ll+1, r, pos+1, rr);
}
return root;
};
void leavel(struct node *root) {
if(!root) return ;
printf("%d", root->n);
queue<struct node *> q;
q.push(root);
while(!q.empty()) {
struct node *t = q.front();
q.pop();
if(t) {
if(t->right) {
q.push(t->right);
printf(" %d", t->right->n);
}
if(t->left) {
q.push(t->left);
printf(" %d", t->left->n);
}
}
}
}
int main() {
scanf("%d", &num);
for(int i = 0; i < num; i++) {
scanf("%d", &zhong[i]);
}
for(int i = 0; i < num; i++) {
scanf("%d", &qian[i]);
}
struct node * root = creatTree(0, num-1, 0, num-1);
leavel(root);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  二叉树 遍历