PAT 1020(由二叉树的中序和后序,求层序遍历) 1086(由二叉树的中序和先序求后序遍历)

10 篇文章 0 订阅
4 篇文章 0 订阅

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
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<iostream>
using namespace std;
#define inf 0x3fffffff
#define LL long long
#define mem(a,b) memset(a,b,sizeof(a))
const int N=35;
int n,num,pos[N],in[N];
struct node
{
    int data;
    node *lchild,*rchild;
};
注意建树的返回类型:node*,四个参数,后序的左右断点以及中序的左右短点
通过两种遍历顺序来找到根节点
node* creat(int posl,int posr,int inl,int inr)
{
    if(posl>posr) return NULL;
    node* root=new node;//定义一个新的节点
    root->data=pos[posr];
    int i;
    for(i=inl;i<=inr;i++)
        if(pos[posr]==in[i])
            break;
    int numl=i-inl;
    root->lchild=creat(posl,posl+numl-1,inl,i-1);//节点的左儿子
    root->rchild=creat(posl+numl,posr-1,i+1,inr);//节点的右儿子
    return root;//返回节点,形成一棵二叉树
}
void bfs(node *root)
{
    num=0;//用来控制格式
    queue<node*>q;//注意队列类型:node*
    q.push(root);
    while(!q.empty())
    {
        node *now=q.front();
        q.pop();
        printf("%d",now->data);
        num++;
        if(num<n) printf(" ");
        else printf("\n");
        if(now->lchild!=NULL) q.push(now->lchild);
        if(now->rchild!=NULL) q.push(now->rchild);
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&pos[i]);
    for(int i=0;i<n;i++)
        scanf("%d",&in[i]);
    node* root=creat(0,n-1,0,n-1);//建树
    bfs(root);//层序遍历
}

上面的代码使用指针实现的,,这里给出数组模拟的代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<stack>
using namespace std;
const int N=35;
int pos[N],in[N],n,tot;
bool flag;
int data[N],lchild[N],rchild[N];
void creat(int &root,int posl,int posr,int inl,int inr)
{
    if(posl>posr) return ;
    root=tot++;
    data[root]=pos[posr];
    int i;
    for(i=inl;i<=inr;i++)
        if(pos[posr]==in[i])
            break;
    int numl=i-inl;
    creat(lchild[root],posl,posl+numl-1,inl,i-1);
    creat(rchild[root],posl+numl,posr-1,i+1,inr);
}
void bfs(int root)
{
    flag=true;
    queue<int>q;
    q.push(root);
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        if(flag) printf("%d",data[now]);
        else printf(" %d",data[now]);
        flag=false;
        if(lchild[now]!=-1) q.push(lchild[now]);
        if(rchild[now]!=-1) q.push(rchild[now]);
    }
    printf("\n");
}
int main()
{
    while(~scanf("%d",&n))
    {
        tot=0;
        memset(lchild,-1,sizeof(lchild));
        memset(rchild,-1,sizeof(rchild));
        for(int i=0;i<n;i++)
            scanf("%d",&pos[i]);
        for(int i=0;i<n;i++)
            scanf("%d",&in[i]);
        int root;
        creat(root,0,n-1,0,n-1);
        bfs(root);
    }
}


1086. Tree Traversals Again (25)

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<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<iostream>
using namespace std;
const int N=35;
int n,num,pre[N],in[N],t[N];
struct node
{
    int data;
    node *lchild,*rchild;
};
node* creat(int prel,int prer,int inl,int inr)
{
    if(prel>prer) return NULL;
    node *root=new node;
    root->data=pre[prel];
    int i;
    for(i=inl;i<=inr;i++)
        if(in[i]==pre[prel])
            break;
    int numl=i-inl;
    root->lchild=creat(prel+1,prel+numl,inl,i-1);
    root->rchild=creat(prel+numl+1,prer,i+1,inr);
    return root;
}
void pos(node* root)
{
    if(root==NULL) return ;
    pos(root->lchild);
    pos(root->rchild);
    num++;//这句话一定要放在两个递归的后面以控制格式
    printf("%d",root->data);
    if(num<n) printf(" ");
    else printf("\n");
}
int main()
{
    num=0;
    int tmp=0,k=0,kk=0;
    char s[5];
    scanf("%d",&n);
    for(int i=0;i<2*n;i++)
    {
        scanf("%s",s);
        if(s[1]=='u')
        {
            scanf("%d",&pre[k++]);
            t[tmp++]=pre[k-1];//t数组模拟栈
        }
        else if(s[1]=='o')
            in[kk++]=t[--tmp];
    }
    node *root=creat(0,n-1,0,n-1);
    pos(root);
}
调用stl栈的代码(主函数):
int main()
{
    char s[5];
    stack<int> st;
    int k=0,kk=0,data;
    scanf("%d",&n);
    for(int i=0;i<2*n;i++)
    {
        scanf("%s",s);
        if(s[1]=='u')
        {
            scanf("%d",&data);
            pre[k++]=data;
            st.push(data);
        }
        else
        {
            in[kk++]=st.top();
            st.pop();
        }
    }
    node *root=creat(0,n-1,0,n-1);
    pos(root);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值