当前位置:网站首页>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
边栏推荐
- C# Tcp服务器如何限制同一个IP的连接数量?
- Look! In June, 2022, the programming language ranking list was released! The first place is awesome
- 正则表达式:语法
- H5 liquid animation JS special effect code
- 剑指Offer || :栈与队列(简单)
- Parameter estimation -- Chapter 7 study report of probability theory and mathematical statistics (point estimation)
- [array] sword finger offer II 012 The sum of left and right subarrays is equal | sword finger offer II 013 Sum of two dimensional submatrix
- Memcached basics 11
- How does the brain do arithmetic? Both addition and subtraction methods have special neurons, and the symbol text can activate the same group of cell sub journals
- 2022 operation of simulated examination platform for tea artist (Senior) work license question bank
猜你喜欢
Detailed explanation of ThreadLocal
Flink learning 1: Introduction
学习太极创客 — MQTT 第二章(一)QoS 服务质量等级
Google began to roll itself, AI architecture pathways was blessed, and 20billion generation models were launched
Parameter estimation -- Chapter 7 study report of probability theory and mathematical statistics (point estimation)
使用命令行安装达梦数据库
【一起上水硕系列】Day 6
【数组】剑指 Offer II 012. 左右两边子数组的和相等 | 剑指 Offer II 013. 二维子矩阵的和
SQLite reader plug-in tests SQLite syntax
[array] sword finger offer II 012 The sum of left and right subarrays is equal | sword finger offer II 013 Sum of two dimensional submatrix
随机推荐
How do I simplify the development of interfaces in open source systems?
2022年氯碱电解工艺试题及答案
paddlepaddle 19 动态修改模型的最后一层
pytorch_grad_cam——pytorch下的模型特征(Class Activation Mapping, CAM)可视化库
Learning Tai Chi Maker - mqtt (VII) advanced mqtt theme
CVPR2022 | PointDistiller:面向高效紧凑3D检测的结构化知识蒸馏
学习太极创客 — MQTT(七)MQTT 主题进阶
Oracle/PLSQL: NumToDSInterval Function
Quicksand painting simulator source code
学习太极创客 — MQTT 第二章(三)保留消息
pytorch 22 8种Dropout方法的简介 及 基于Dropout用4行代码快速实现DropBlock
Flink學習2:應用場景
docker部署redis集群
Yiwen teaches you Kali information collection
mmdetection ValueError: need at least one array to concatenate解决方案
Super détaillé, 20 000 caractères détaillés, mangez à travers es!
Constraintlayout Development Guide
Flink learning 2: application scenarios
Laravel 的 ORM 缓存包
Shell script series (1) getting started