给出一棵二叉树的后序遍历序列和中序遍历序列,求这棵二叉树的层序遍历序列

  1. #include<iostream>
  2. #include<cstring>
  3. #include<queue>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. const int maxn = 50;
  8. struct node
  9. {
  10. int data;
  11. node* lchild;
  12. node* rchild;
  13. };
  14.  
  15. int pre[maxn],in[maxn],post[maxn];//先序,中序,后序
  16. int n;//结点个数
  17. //当前二叉树的后序序列区间为[postl,postr],中序序列的区间为[inl,inr]
  18. //create函数返回构建出的二叉树的根节点
  19.  
  20. node* create(int postL,int postR,int inL,int inR)
  21. {
  22. if(postL>postR)
  23. {
  24. return NULL;//后序序列长度小于等于1时直接返回
  25. }
  26. node* root =new node;//新建一个节点,用来存放当前二叉树的根节点
  27. root->data =post[postR];//新节点的数据域为根节点的值
  28. int k;
  29. for(k=inL;k<=inR;k++)
  30. {
  31. if(in[k]==post[postR])
  32. {
  33. break;
  34. }
  35. }
  36. int numLeft = k-inL;//左子树节点的数目
  37. //返回左子树的根节点的地址,赋值给root的左指针
  38. root->lchild = create(postL,postL+numLeft-1,inL,k-1);
  39. //返回右子树的根节点的地址,赋值给root的右指针
  40. root->rchild = create(postL+numLeft,postR-1,k+1,inR);
  41. return root;//返回根节点的地址
  42. }
  43.  
  44. int num =0; //已经输出的结点的个数
  45. void BFS(node* root)
  46. {
  47. queue<node*> q;//队列里面是存放地址
  48. q.push(root);//将根节点的地址入队
  49. while(!q.empty())
  50. {
  51. node* now = q.front();//取出队首元素
  52. q.pop();
  53. printf("%d",now->data);//访问队首元素
  54. num++;
  55. if(num<n) printf(" ");
  56. if(now->lchild != NULL) q.push(now->lchild);//左子树非空
  57. if(now->rchild != NULL) q.push(now->rchild);//右子树
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. scanf("%d",&n);
  64. for(int i=0;i<n;i++)
  65. {
  66. scanf("%d",&post[i]);
  67. }
  68. for(int i=0;i<n;i++)
  69. {
  70. scanf("%d",&in[i]);
  71. }
  72. node* root = create(0,n-1,0,n-1);//建树
  73. BFS(root); //层序遍历
  74. return 0;
  75. }

  

PAT A 1020 Tree Traversals的更多相关文章

  1. 【PAT】1020 Tree Traversals (25)(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  2. PAT 甲级 1020 Tree Traversals (二叉树遍历)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  3. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  4. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  5. PAT Advanced 1020 Tree Traversals (25 分)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  6. 【PAT】1020. Tree Traversals (25)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  7. PAT 甲级 1020 Tree Traversals

    https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...

  8. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  9. PAT 1020. Tree Traversals

    PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...

随机推荐

  1. python自动更新升级失败解决方案

    1,使用python -m pip install --upgrade pip升级失败 2,使用python -m pip install -U --force-reinstall pip依然失败 3 ...

  2. JAVA基础学习(3)之循环

    3循环 3.1循环 3.1.1循环 一直要做的行为进行循环 3.1.2数数字 while(){}判断是否进行 数数字:number/10 //数数字Scanner in = new Scanner(S ...

  3. PS绘制Logo

    1. 2. 3. 4. 5. 6. 第2步点击“圆1”应点击图层左边的缩览图才能获取选区 7. 8. 9. 10. 11. 12.

  4. Codeforces Round #601 (Div. 2)D(蛇形模拟)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; vector<char>an ...

  5. crawlSpider全站爬取 分布式

    # 如何提升scrapy爬取数据的效率? 推荐: 单线程加异步协程 增加并发: 默认scrapy开启的并发线程为32个,可以适当进行增加.在settings.py中修改 CONCURRENT_REQU ...

  6. CSS实现心形、六角星、六边形、平行四边形等几何

    本文将利用border属性实现简单几何的绘制: 效果图: 正八角星 说明:采用两个正方形以中心进行旋转叠加: /* 八角星 */ #burst-8 { background: #6376ff1f; w ...

  7. 定义列表dl中标签 dt 与标签dd对齐方法,标签ul与标签li对齐

    不定义css样式时(默认情况): 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...

  8. XMPP详解

    https://www.jianshu.com/p/84d15683b61e https://www.cnblogs.com/lurenq/p/7026983.html 1. xmpp简介 XMPP ...

  9. es 分词器介绍

    按照单词切分,不做处理 GET _analyze { "analyzer": "standard", "text": "2 run ...

  10. P1893山峰瞭望

    传送门 看完这个题,大家都懂意思吧,然后代码呢,emmmmm #include <bits/stdc++.h> using namespace std; const int maxn = ...