【1127】ZigZagging on a Tree (30分)【中后序建树 层次】

简介: 【1127】ZigZagging on a Tree (30分)【中后序建树 层次】【1127】ZigZagging on a Tree (30分)【中后序建树 层次】
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>  
#include<map>
#include<vector>
#include<queue> 
#include<string>
using namespace std;  
vector<int>postorder(31);
vector<int>inorder(31);
vector<vector<int>>levels(31);
void buildTree(int postl,int postr,int inl,int inr,int level){
    if(postl>postr || inl>inr)   return;
    int root=postorder[postr];//root为根结点,在后序遍历最后一个位置上
    int i=0;
    while(inorder[inl+i] !=root)//在中序遍历序列中查找根结点位置  
      i++;//跳出while时得到i,左子树结点个数为i
    levels[level].push_back(root);//把根结点存入对应层的vector中
    buildTree(postl,  postl+i-1,  inl,  inl+i-1,  level+1);//建立左子树      
    buildTree(postl+i,  postr-1,  inl+i+1,  inr,  level+1);//建立右子树     
    //后序遍历:  左子树(postl ...postl+i-1)  右子树(post+i ... postr-1)  root=postr-1 
    //中序遍历 : 左子树(inl...inl+i-1)            root=inl+i                        右子树(inl+i+1...inr)
}
void zigzag(){//Z型的层序遍历
  cout<<levels[0][0];
  bool zigzag=false;
  for(int i=1;i<levels.size()&& !levels[i].empty();i++){
    //注意levels.size()为树的层数  并且判断了当前层的结点数不为空的时候
    if(zigzag){
      for(int j=levels[i].size()-1;j>=0;j--){
        cout<<" "<<levels[i][j];
      }
    }
    else{
      for(int j=0;j<levels[i].size();j++){
        cout<<" "<<levels[i][j];
      }
    }
    zigzag=!zigzag;
  }
}
int main(){
  int N;
  while(cin>>N){
    for(int i=0;i<N;i++){
      cin>>inorder[i];//存入中序遍历序列
    }
    for(int i=0;i<N;i++){
      cin>>postorder[i];//存入后序遍历序列
    }
    buildTree(0,N-1,0,N-1,0);
    zigzag();
  }
  system("pause");
  return 0;
}

和晴神差不多的版本

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>  
#include<map>
#include<vector>
#include<queue> 
using namespace std;  
int n;
int in[40],post[40];
struct node{
  int data;
  int level;
  node* left;
  node* right;
  node():left(NULL),right(NULL){}
};
node* create(int inL,int inR,int postL,int postR){
  if(inL>inR) return NULL;
  node* root=new node;
  //新建一个新的结点,用来存放当前二叉树的根结点
  root->data=post[postR];
  //新结点的数据域为根结点的值
  int k;
  for(k=inL;k<=inR;k++){
    if(in[k]==post[postR]){//在中序序列中找到in[k]==post[postR]的结点
      break;
    }
  }
  int num_Left=k-inL;//左子树的结点个数
  //返回左子树的根结点地址,赋值给root的左指针
  root->left=create(inL,k-1,postL,postL+num_Left-1);
  root->right=create(k+1,inR,postL+num_Left,postR-1);
  //返回右子树的根结点地址,赋值给root的右指针
  return root;
}
 int max_level=0;
 vector<int> ve[40];
 void BFS(node* root){
  queue<node*>q;
  root->level=1;//一开始漏了这句,导致跳出读取位置 xxx时发生访问冲突 的报错
  q.push(root);
  while(!q.empty()){
    node* now=q.front();
    q.pop();
    ve[now->level].push_back(now->data);
    if(now->level>max_level) max_level=now->level;
    if(now->left!=NULL){
      now->left->level=now->level+1;
      q.push(now->left);
    }
    if(now->right!=NULL){
      now->right->level=now->level+1;
      q.push(now->right);
    }
  }
 }
int main(){   
  int i,j,total=0;
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    scanf("%d",&in[i]);
  }
  for(int i=0;i<n;i++){
    scanf("%d",&post[i]);
  }
  node* root=create(0,n-1,0,n-1);
  BFS(root);
  for(int i=1;i<=max_level;i++){
    if(i==1||i%2==0){
      for(int j=0;j<ve[i].size();j++){
        printf("%d",ve[i][j]);
        total++;
        if(total<n) printf(" ");
      }
    }else{
      for(int j=ve[i].size()-1;j>=0;j--){
        printf("%d",ve[i][j]);
        total++;
        if(total<n)  printf(" ");
      }
    }
  }
  system("pause"); 
    return 0;   
}
相关文章
|
Ubuntu 编译器 开发工具
在Ubuntu系统上搭建RISC-V交叉编译环境
以上步骤涵盖了在Ubuntu系统上搭建RISC-V交叉编译环境的主要过程。这一过程涉及了安装依赖、克隆源码、编译安装工具链以及设置环境变量等关键步骤。遵循这些步骤,可以在Ubuntu系统上搭建一个用于RISC-V开发的强大工具集。
1193 22
ENVI Classic:如何加载栅格数据(Img/DEM)和矢量数据(evf of ROI)?
ENVI Classic:如何加载栅格数据(Img/DEM)和矢量数据(evf of ROI)?
2061 0
|
Ubuntu Linux Windows
如何在Ubuntu系统中安装Wine,借此来运行Windows程序
熟悉的登录画面出现,在Ubuntu系统中扫描登录微信程序。
|
机器学习/深度学习 人工智能 安全
Gandalf AI 通关详解(大模型安全)
Gandalf AI 通关详解(大模型安全)
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8057 2
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18276 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全
|
2天前
|
人工智能 弹性计算 安全
阿里云 码上2027 AI coding 嘉年华
阿里云“码上2027”AI Coding嘉年华于2026年12月2—3日在上海西岸艺术中心举行,聚焦AI Coding从单点提效迈向企业级研发体系升级。发布Qwen4-max模型、QBot智能协作、AgentCore平台等重磅成果,并推出ECS云服务器特惠(200Mbps流量月付40元起)。
774 0