二叉树的层次遍历

来源:互联网 发布:逆战混沌16连炮数据 编辑:程序博客网 时间:2024/06/05 19:23

终于自己能够写出二叉树的前序, 中序, 后序和层次遍历了,还是有点开心的。才发现原来建树是那么简单,只要理解好递归就差不多了。

之前转过前中后序的遍历,现在就记下层次遍历吧。

用pat上的题当例子吧。

https://www.patest.cn/contests/gplt/L2-006

L2-006. 树的遍历

 

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

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

输入格式:

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

输出格式:

在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:
72 3 1 5 7 6 41 2 3 4 5 6 7
输出样例:
4 1 6 3 5 7 2
一开始用的是一个一维数组来建树,即tree[1]为根节点,tree[i]的左儿子是tree[2*i],右儿子是tree[2*i+1].
#include<iostream>#include<cstdio>#include<queue>#include<cstring>using namespace std;const int maxn = 105;//the size of maxn should be 2^(N+1)-1int tree[maxn], in[maxn], post[maxn];void build(int rt, int n, int *in, int *post){    if(n <= 0) return ;    int p = 0;    while(in[p] != post[n-1]) p++;    tree[rt] = post[n-1];    //cout << post[n-1] << endl;    //print preOrder    build(rt<<1, p, in, post);    build(rt<<1|1, n-p-1, in+p+1, post+p);}void bfs(void){    queue<int>q;    int flag = 0, root = 1;    q.push(root);    while(q.size())    {        if(flag++) cout << ' ';        root = q.front(); q.pop();        cout << tree[root];        if(tree[root<<1] != -1) q.push(root<<1);        if(tree[root<<1|1] != -1) q.push(root<<1|1);    }}int main(void){    int n;    cin >> n;    for(int i = 0; i < n; i++) cin >> post[i];    for(int i = 0; i < n; i++) cin >> in[i];    memset(tree, -1 ,sizeof(tree));    build(1, n, in, post);  //roor node is tree[1]    bfs();    return 0;}


这个方法只用了一个数组来描述树, 但需要开很大很大的数组, 很浪费空间, 需要开到2^(N+1)-1, 结点数一多就不能用这种方法。

像这样:

 

方法二:用两个数组记录某结点的左右孩子

#include<iostream>#include<cstdio>#include<queue>#include<cstring>using namespace std;const int maxn = 35;int post[maxn], in[maxn], lch[maxn], rch[maxn];int build(int n, int *in, int *post){    if(n <= 0) return -1;    int root = post[n-1];    int p = 0;    while(in[p] != root) p++;    lch[root] = build(p, in, post);    rch[root] = build(n-p-1, in+p+1, post+p);    return root;}void bfs(int root){    queue<int>q;    q.push(root);    int flag = 0;    while(q.size())    {        if(flag++) cout << ' ';        root = q.front(); q.pop();        cout << root;        if(lch[root] != -1) q.push(lch[root]);        if(rch[root] != -1) q.push(rch[root]);    }}int main(void){    int n;    cin >> n;    for(int i = 0; i < n; i++) cin >> post[i];    for(int i = 0; i < n; i++) cin >> in[i];    int root = build(n, in, post);    bfs(root);    return 0;}


这个方法数组大小只需要N即可。

 

pat上还有一题,镜像层序遍历,其实只要在建树时讲左右孩子互换即可。

https://www.patest.cn/contests/gplt/L2-011

L2-011. 玩转二叉树

 

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

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

输入格式:

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

输出格式:

在一行中输出该树反转后的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

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

 

同样贴上两个代码

一:

#include<iostream>#include<cstdio>#include<queue>#include<cstring>using namespace std;//maxn should be very big!!const int maxn = 10005;int tree[maxn], in[maxn], pre[maxn];void build(int rt, int n, int *in, int *pre){    if(n <= 0) return ;    int p = 0;    while(in[p] != pre[0]) p++;    tree[rt] = pre[0];    build(rt<<1, n-p-1, in+p+1, pre+p+1);    build(rt<<1|1, p, in, pre+1);}void bfs(void){    queue<int>q;    int flag = 0, root = 1;    q.push(root);    while(q.size())    {        if(flag++) cout << ' ';        root = q.front();        q.pop();        cout << tree[root];        if(tree[root<<1] != -1) q.push(root<<1);        if(tree[root<<1|1] != -1) q.push(root<<1|1);    }}int main(void){    int n;    cin >> n;    for(int i = 0; i < n; i++) cin >> in[i];    for(int i = 0; i < n; i++) cin >> pre[i];    memset(tree, -1 ,sizeof(tree));    build(1, n, in, pre);    bfs();    return 0;}


二:

#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int maxn = 35;int pre[maxn], in[maxn], lch[maxn], rch[maxn];int build(int n, int *pre, int *in){    if(n <= 0) return -1;    int root = pre[0], p = 0;    while(in[p] != root) p++;    rch[root] = build(p, pre+1, in);    lch[root] = build(n-p-1, pre+1+p, in+p+1);    return root;}void bfs(int root){    queue<int>q;    q.push(root);    int flag = 0;    while(q.size())    {        if(flag++) cout << ' ';        root = q.front(); q.pop();        cout << root;        if(lch[root] != -1) q.push(lch[root]);        if(rch[root] != -1) q.push(rch[root]);    }}int main(void){    int n;    cin >> n;    for(int i = 0; i < n; i++) cin >> in[i];    for(int i = 0; i < n; i++) cin >> pre[i];    int root = build(n, pre, in);    bfs(root);    return 0;}


还可以用结构体存树,但相对来说会麻烦一点。

1 0
原创粉丝点击