当前位置:网站首页>Pat grade a 1020 tree Traversals
Pat grade a 1020 tree Traversals
2022-06-27 02:51:00 【IX. non random address】
The middle order traversal sequence of binary tree , And post order traversal sequence , Find sequence traversal sequence
Middle order traversal sequence :[left, root, right], Post traversal sequence :[left, right, root]
postorder: 4 5 2 3 1 inorder: 4 2 5 1 3

First look at the following traversal sequence , The last one is root node 1, then inorder Can be 1 To the left and right
obtain 4 2 5 and 3; Then according to the element ,postorder Can be reduced to 4 5 2 and 3.1 Deleted
take 1 Stored in a binary tree structure value, then postorder Turn into 2 A the ,inorder Also become a 2 A the
postorder1: 4 5 2 inorder1: 4 2 5
postorder2: 3 inorder2: 3
Then according to the subsequent traversal, the last 1 position , You can get the root node root1=2, then inorder1 By 2 Cut away , Continue to divide the left branch into two
postorder3: 4 inorder 4
postorder4: 5 inorder4: 5
The overall structure of the binary tree can be obtained recursively , Pay attention to the binary tree assignment in advance malloc Memory
#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
边栏推荐
- Oracle/PLSQL: Upper Function
- Enterprise digital transformation: informatization and digitalization
- Paddlepaddle 21 is implemented based on dropout with 4 lines of code droplock
- 解决cherry pick提交报错问题
- Don't be brainwashed. This is the truth about the wages of 90% of Chinese people
- Oracle/PLSQL: From_ Tz function
- H5 liquid animation JS special effect code
- Docker deploy redis cluster
- Shell script series (1) getting started
- paddlepaddle 20 指数移动平均(ExponentialMovingAverage,EMA)的实现与使用(支持静态图与动态图)
猜你喜欢

Hot discussion: what are you doing for a meaningless job with a monthly salary of 18000?

Mmdetection valueerror: need at least one array to concatenate solution

Introduction to stm32

I earned 3W yuan a month from my sideline: the industry you despise really makes money!

Flink learning 4:flink technology stack

Parameter estimation -- Chapter 7 study report of probability theory and mathematical statistics (point estimation)

Would rather go to 996 than stay at home! 24 years old, unemployed for 7 months, worse than work, no work

超级详细,2 万字详解,吃透 ES!

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

H5 liquid animation JS special effect code
随机推荐
Cvpr2022 | pointdistiller: structured knowledge distillation for efficient and compact 3D detection
pytorch_grad_cam——pytorch下的模型特征(Class Activation Mapping, CAM)可视化库
[micro service sentinel] degradation rules slow call proportion abnormal proportion abnormal constant
Hot discussion: what are you doing for a meaningless job with a monthly salary of 18000?
【数组】剑指 Offer II 012. 左右两边子数组的和相等 | 剑指 Offer II 013. 二维子矩阵的和
谷歌开始卷自己,AI架构Pathways加持,推出200亿生成模型
Flink learning 2: application scenarios
Getting started with bluecms code auditing
Flink learning 4:flink technology stack
学习太极创客 — MQTT(八)ESP8266订阅MQTT主题
Oracle/PLSQL: CharToRowid Function
2022茶艺师(高级)上岗证题库模拟考试平台操作
2022中式面点师(高级)复训题库及在线模拟考试
Don't be brainwashed. This is the truth about the wages of 90% of Chinese people
学习太极创客 — MQTT 第二章(二)ESP8266 QoS 应用
ORM cache package for laravel
pytorch 23 hook的使用与介绍 及基于hook实现即插即用的DropBlock
栈溢出漏洞
Sample development of WiFi IOT Hongmeng development kit
Flink学习1:简介
https://github.com/ZouJiu1/PAT