小峰峰的pat甲级刷题记录1020

小峰峰的pat甲级刷题记录1020

方法一:通过后序和中序序列构建树,再层序输出

#include<iostream>
#include<vector>
using namespace std;

struct node
{
    int value;
    node* left,*right;
};
int N;
vector<int>Post;
vector<int>In;
node* makenode(int h1,int t1,int h2,int t2)
{
    if(h1>t1)return NULL;  // 死路返回(h1==t1时传入一棵节点数为1的树)
    node* p = new node;
    p->value = Post[t1];
    int i;
    for(i=h2;In[i]!=Post[t1];i++);
    p->left=makenode(h1,i-1-h2+h1,h2,i-1);  //  同一棵树节点数相等,所以i-1-h2=x-h1 求出x=i-1-h2+h1 
    p->right=makenode(i+t1-t2,t1-1,i+1,t2);
    return p;
}

int main()
{
    cin>>N;
    int i;
    Post.resize(N);
    In.resize(N);
    for(i=0;i<N;i++)cin>>Post[i];
    for(i=0;i<N;i++)cin>>In[i];
    node* root = makenode(0,N-1,0,N-1);
    node* Queue[40];
    int head=0,tall=0;  // head==tall时队列为空
    Queue[tall++]=root;  // tall指针永远为空
    int j=0;
    while(head<tall)
    {
        node* p=Queue[head++];
        if(j==0)cout<<p->value;
        else cout<<' '<<p->value;
        j=1;
        if(p->left)Queue[tall++]=p->left;
        if(p->right)Queue[tall++]=p->right;
    }
}

模块&拓展

用先序序列与中序序列构建树

node* makenode(int h1,int t1,int h2,int t2)
{
    if(h1>t1)return NULL;  // 死路返回
    node* p = new node;
    p->value = Pre[h1];
    int i;
    for(i=h2;In[i]!=Post[h1];i++);
    p->left=makenode(h1+1,i-h2+h1,h2,i-1);  //  同一棵树节点数相等,所以i-1-h2=x-h1 求出x=i-h2+h1 
    p->right=makenode(t1-t2+i+1,t1,i+1,t2);
    return p;
}

只传三个参数构建树

node* makenode(int root,int In_h,int In_t)
{
    if(In_h>In_t)return NULL;  // 死路返回
    node* p = new node;
    p->value = Post[t1];
    int i;
    for(i=h2;In[i]!=Post[t1];i++);
    p->left=makenode(root-1-(In_t-i),In_h,i-1); 
    p->right=makenode(root-1,i+1,In_t);
    return p;
}

不用queue库实现队列

node* Queue[40];
    int head=0,tall=0;  // head==tall时队列为空
    Queue[tall++]=root;  // tall指针永远为空
    int j=0;
    while(head<tall)
    {
        node* p=Queue[head++];
        if(j==0)cout<<p->value;
        else cout<<' '<<p->value;
        j=1;
        if(p->left)Queue[tall++]=p->left;
        if(p->right)Queue[tall++]=p->right;
    }

用queue库

	int j=0;
    while(!que.empty())
    {
        node* p=que.front();
        que.pop();
        if(j==0)cout<<p->value;
        else cout<<' '<<p->value;
        j=1;
        if(p->left)que.push(p->left);
        if(p->right)que.push(p->right);
    }

方法二:不构建树

#include <cstdio>
#include <vector>
#include <map>
using namespace std;
vector<int> post, in;
map<int, int> level;
void pre(int root, int start, int end, int index) {
    if(start > end) return ;
    int i = start;
    while(i < end && in[i] != post[root]) i++;
    level[index] = post[root];
    pre(root - 1 - end + i, start, i - 1, 2 * index + 1);
    pre(root - 1, i + 1, end, 2 * index + 2);
}
int main() {
    int n;
    scanf("%d", &n);
    post.resize(n);
    in.resize(n);
    for(int i = 0; i < n; i++) scanf("%d", &post[i]);
    for(int i = 0; i < n; i++) scanf("%d", &in[i]);
    pre(n-1, 0, n-1, 0);
    auto it = level.begin();
    printf("%d", it->second);
    while(++it != level.end()) printf(" %d", it->second);
    return 0;

————————————————
版权声明:本文为CSDN博主「柳婼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/liuchuo/article/details/52137796

模块&拓展

通过后续序列和中序序列直接实现先序遍历

void pre(int root,int In_h,int In_t)
{
    if(In_h>In_t)return;  //由于是void所以return后什么都不写
    cout<<Post[root];
    int j;
    for(j=In_h;In[j]!=Post[root];j++);
    pre(root-1-(In_t-j),In_h,j-1);
    pre(root-1,j+1,In_t);
}

当给出前中序求后序序列时,只需要改变cout语句的位置,就能获得后序序列

利用字典的按键值自动排序功能求得层序序列

void pre(int root,int In_h,int In_t,int index)
{
    if(In_h>In_t)return ;  // 死路返回
    level[index]=Post[root];
    for(int j=In_h;In[j]!=Post[root];j++);
    pre(root-1-(In_t-j),In_h,j-1,2*index+1);
    pre(root-1,j+1,In_t,2*index+2);    
}

遍历字典的方法

//第一种用指针
auto it = level.begin();
printf("%d", it->second);
while(++it != level.end()) printf(" %d", it->second); // .end()指向空 
//第二种用in
int tem=0;
for(auto it:level)
{
    if(tem==0)
    {
        tem=1; 
        cout<<it.second;
    }
    else cout<<' '<<it.second;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值