当前位置:网站首页>PAT甲级 1020 Tree Traversals
PAT甲级 1020 Tree Traversals
2022-06-27 02:44:00 【九是否非随机的称呼】
已经二叉树的中序遍历序列,以及后序遍历序列,求层序遍历序列
中序遍历序列:[left, root, right],后序遍历序列:[left, right, root]
postorder: 4 5 2 3 1 inorder: 4 2 5 1 3

先看后续遍历序列,最后一位就是root节点1,然后inorder可以被1化为左右两侧
得到4 2 5 和 3;然后相应的根据元素,postorder可化为4 5 2 和 3。1被删除了的
将1存到二叉树结构体的value,然后postorder变为2个了,inorder也变为2个了
postorder1: 4 5 2 inorder1: 4 2 5
postorder2: 3 inorder2: 3
然后根据后续遍历最后1位,可以得到根节点root1=2,然后inorder1被2划开,将左支继续划为两支
postorder3: 4 inorder 4
postorder4: 5 inorder4: 5
可以使用递归的方式得到二叉树的整体结构,要注意二叉树赋值要提前malloc内存
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<bits/stdc++.h>
using namespace std;
int x, y, z;
struct nodees{
struct nodees *left;
struct nodees *right;
int values;
};
void gettree(nodees *nd, vector<int> v, vector<int> v0){
if(v.size()==0||v0.size()==0) return;
x = v[v.size()-1];
nd->values = x;
vector<int> left0, right0, tmp, left, right;
tmp = v0;
for(int i=0; i<v0.size(); i++){
if(v0[i]==x){
y = i;
break;
}
}
v.pop_back();
v0.erase(v0.begin() + y, v0.end());
left0 = v0;
set<int> st;
set<int>::iterator it;
for(int i = 0; i < left0.size(); i++) st.insert(left0[i]);
tmp.erase(tmp.begin(), tmp.begin() + y + 1);
right0 = tmp;
for(int i = 0; i < v.size(); i++){
it = st.find(v[i]);
if(it==st.end()){
y = i;
break;
}
}
tmp = v;
v.erase(v.begin() + y, v.end());
left = v;
tmp.erase(tmp.begin(), tmp.begin() + y);
right = tmp;
nd->left = (struct nodees *)malloc(sizeof(struct nodees));
nd->right = (struct nodees *)malloc(sizeof(struct nodees));
nd->left->values = -9;
nd->right->values = -9;
gettree(nd->left, left, left0);
gettree(nd->right, right, right0);
}
int main(){
int n, i, j, a, c;
cin>>n;
vector<int> v, v0;
map<int, int> m, m0;
struct nodees *nd = (struct nodees *)malloc(sizeof(struct nodees));
for(i = 0; i < n; i++){
cin>>a;
v.push_back(a);
m[a] = i;
}
for(i = 0; i < n; i++){
cin>>a;
v0.push_back(a);
m0[a] = i;
}
gettree(nd, v, v0);
vector<long long> add;
add.push_back((long long)nd);
struct nodees *ndd;
vector<int> www;
while(add.size()!=0){
ndd = (struct nodees *)add[0];
add.erase(add.begin());
www.push_back(ndd->values);
if(ndd->left->values > 0) add.push_back((long long)ndd->left);
if(ndd->right->values > 0) add.push_back((long long)ndd->right);
}
for(int i = 0; i < www.size(); i++){
cout<<www[i];
if(i!=(www.size()-1)) cout<<" ";
}
return 0;
}Tree Traversals (Inorder, Preorder and Postorder) - GeeksforGeeks
边栏推荐
- 达梦数据库安装
- canvas粒子篇之鼠标跟随js特效
- Learn Tai Chi Maker - mqtt (VIII) esp8266 subscribe to mqtt topic
- docker部署redis集群
- 学习太极创客 — MQTT(七)MQTT 主题进阶
- H5 liquid animation JS special effect code
- Brief introduction of 228 dropout methods of pytorch and fast implementation of dropblock with 4 lines of code based on dropout
- JWT certification process and use cases
- Detailed explanation of ThreadLocal
- Memcached basics 11
猜你喜欢

Brief introduction of 228 dropout methods of pytorch and fast implementation of dropblock with 4 lines of code based on dropout

学习太极创客 — MQTT(八)ESP8266订阅MQTT主题

How does source insight (SI) display the full path? (do not display omitted paths) (turn off trim long path names with ellipses)

Mmdetection uses yolox to train its own coco data set

学习太极创客 — MQTT 第二章(一)QoS 服务质量等级

Learn Tai Chi Maker - mqtt (VI) esp8266 releases mqtt message

Web development framework - Express (installation and use, static hosting, routing processing, use of Middleware)

STM32入门介绍

Learn from Taiji Maker - mqtt Chapter 2 (I) QoS service quality level

Super détaillé, 20 000 caractères détaillés, mangez à travers es!
随机推荐
CVPR2022 | PointDistiller:面向高效紧凑3D检测的结构化知识蒸馏
Learn from Taiji Maker - mqtt Chapter 2 (I) QoS service quality level
Learn Tai Chi Maker - mqtt (VIII) esp8266 subscribe to mqtt topic
Brief introduction of 228 dropout methods of pytorch and fast implementation of dropblock with 4 lines of code based on dropout
dat.gui.js星星圆圈轨迹动画js特效
Regular expressions: Syntax
paddlepaddle 19 动态修改模型的最后一层
Calculation of average wind direction and speed (unit vector method)
Quicksand painting simulator source code
学习太极创客 — MQTT(八)ESP8266订阅MQTT主题
一文教你Kali信息收集
执念斩长河暑期规划
How do I simplify the development of interfaces in open source systems?
Flink learning 1: Introduction
Paddlepaddle 20 implementation and use of exponentialmovingaverage (EMA) (support static graph and dynamic graph)
【一起上水硕系列】Day 6
Flink学习1:简介
Oracle/PLSQL: Rpad Function
Learning Tai Chi Maker - mqtt Chapter 2 (II) esp8266 QoS application
Topolvm: kubernetes local persistence scheme based on LVM, capacity aware, dynamically create PV, and easily use local disk
https://github.com/ZouJiu1/PAT