PAT A 1020 Tree Traversals
给出一棵二叉树的后序遍历序列和中序遍历序列,求这棵二叉树的层序遍历序列
- #include<iostream>
- #include<cstring>
- #include<queue>
- #include<algorithm>
- using namespace std;
- const int maxn = 50;
- struct node
- {
- int data;
- node* lchild;
- node* rchild;
- };
- int pre[maxn],in[maxn],post[maxn];//先序,中序,后序
- int n;//结点个数
- //当前二叉树的后序序列区间为[postl,postr],中序序列的区间为[inl,inr]
- //create函数返回构建出的二叉树的根节点
- node* create(int postL,int postR,int inL,int inR)
- {
- if(postL>postR)
- {
- return NULL;//后序序列长度小于等于1时直接返回
- }
- node* root =new node;//新建一个节点,用来存放当前二叉树的根节点
- root->data =post[postR];//新节点的数据域为根节点的值
- int k;
- for(k=inL;k<=inR;k++)
- {
- if(in[k]==post[postR])
- {
- break;
- }
- }
- int numLeft = k-inL;//左子树节点的数目
- //返回左子树的根节点的地址,赋值给root的左指针
- root->lchild = create(postL,postL+numLeft-1,inL,k-1);
- //返回右子树的根节点的地址,赋值给root的右指针
- root->rchild = create(postL+numLeft,postR-1,k+1,inR);
- return root;//返回根节点的地址
- }
- int num =0; //已经输出的结点的个数
- void BFS(node* root)
- {
- queue<node*> q;//队列里面是存放地址
- q.push(root);//将根节点的地址入队
- while(!q.empty())
- {
- node* now = q.front();//取出队首元素
- q.pop();
- printf("%d",now->data);//访问队首元素
- num++;
- if(num<n) printf(" ");
- 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",&post[i]);
- }
- for(int i=0;i<n;i++)
- {
- scanf("%d",&in[i]);
- }
- node* root = create(0,n-1,0,n-1);//建树
- BFS(root); //层序遍历
- return 0;
- }
PAT A 1020 Tree Traversals的更多相关文章
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- 【PAT】1020. Tree Traversals (25)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- PAT 甲级 1020 Tree Traversals
https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...
- PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- PAT 1020. Tree Traversals
PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...
随机推荐
- python自动更新升级失败解决方案
1,使用python -m pip install --upgrade pip升级失败 2,使用python -m pip install -U --force-reinstall pip依然失败 3 ...
- JAVA基础学习(3)之循环
3循环 3.1循环 3.1.1循环 一直要做的行为进行循环 3.1.2数数字 while(){}判断是否进行 数数字:number/10 //数数字Scanner in = new Scanner(S ...
- PS绘制Logo
1. 2. 3. 4. 5. 6. 第2步点击“圆1”应点击图层左边的缩览图才能获取选区 7. 8. 9. 10. 11. 12.
- Codeforces Round #601 (Div. 2)D(蛇形模拟)
#define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; vector<char>an ...
- crawlSpider全站爬取 分布式
# 如何提升scrapy爬取数据的效率? 推荐: 单线程加异步协程 增加并发: 默认scrapy开启的并发线程为32个,可以适当进行增加.在settings.py中修改 CONCURRENT_REQU ...
- CSS实现心形、六角星、六边形、平行四边形等几何
本文将利用border属性实现简单几何的绘制: 效果图: 正八角星 说明:采用两个正方形以中心进行旋转叠加: /* 八角星 */ #burst-8 { background: #6376ff1f; w ...
- 定义列表dl中标签 dt 与标签dd对齐方法,标签ul与标签li对齐
不定义css样式时(默认情况): 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...
- XMPP详解
https://www.jianshu.com/p/84d15683b61e https://www.cnblogs.com/lurenq/p/7026983.html 1. xmpp简介 XMPP ...
- es 分词器介绍
按照单词切分,不做处理 GET _analyze { "analyzer": "standard", "text": "2 run ...
- P1893山峰瞭望
传送门 看完这个题,大家都懂意思吧,然后代码呢,emmmmm #include <bits/stdc++.h> using namespace std; const int maxn = ...