当前位置:网站首页>PTA 7-1 play with binary tree
PTA 7-1 play with binary tree
2022-07-27 00:09:00 【Muxi Krystal】
https://pintia.cn/problem-sets/1259041418971803648/problems/1259046259492220928
( Title link above )
The question :
Given a binary tree in order to traverse and preorder traversal , Please mirror the tree first , And then output the inverted sequence traversal sequence . The so-called mirror reversal , It means to exchange the left and right children of all non leaf nodes .
Use middle order traversal and pre order traversal to build trees :
node* buildPreInTree(int pl,int pr,int il,int ir){
if(il>ir) return NULL;
node *root=new node;
root->num=pre[pl];
int mid;
for(int i=il;i<=ir;i++){
if(pre[pl]==in[i]){
mid=i;break;
}
}
int llen=mid-il,rlen=ir-mid;
root->left=buildPreInTree(pl+1,pl+llen,il,mid-1);
root->right=buildPreInTree(pl+llen+1,pr,mid+1,ir);
return root;
}
In this question , To put root->left and root->right transposition , To build trees .
Sequence traversal of binary tree :
int levelOrderTraverse(node* rt){
queue<node*>q;
q.push(rt);
int k=0;
while(!q.empty()){
node* root=q.front();
q.pop();
lev[k++]=root->num;
if(root->left!=NULL) q.push(root->left);
if(root->right!=NULL) q.push(root->right);
}
return k;
}
Using queues , Considering the output format , Just store it in lev In the array .
AC Code :
#include <iostream>
#include <queue>
using namespace std;
int n,pre[35],in[35],lev[35];
struct node{
int num;
node *left,*right;
};
node* buildPreInTree(int pl,int pr,int il,int ir){
if(il>ir) return NULL;
node *root=new node;
root->num=pre[pl];
int mid;
for(int i=il;i<=ir;i++){
if(pre[pl]==in[i]){
mid=i;break;
}
}
int llen=mid-il,rlen=ir-mid;
root->right=buildPreInTree(pl+1,pl+llen,il,mid-1);
root->left=buildPreInTree(pl+llen+1,pr,mid+1,ir);
return root;
}
int levelOrderTraverse(node* rt){
queue<node*>q;
q.push(rt);
int k=0;
while(!q.empty()){
node* root=q.front();
q.pop();
lev[k++]=root->num;
if(root->left!=NULL) q.push(root->left);
if(root->right!=NULL) q.push(root->right);
}
return k;
}
int main(){
cin>>n;
for(int i=0;i<n;i++){
cin>>in[i];
}
for(int i=0;i<n;i++){
cin>>pre[i];
}
node *rt;
rt=buildPreInTree(0,n-1,0,n-1);
int k;
k=levelOrderTraverse(rt);
for(int i=0;i<k;i++){
if(i==0) cout<<lev[i];
else cout<<" "<<lev[i];
}
cout<<endl;
return 0;
}
边栏推荐
- In simple terms, cchart's daily lesson - Lesson 59 of happy high school 4 comes to the same end by different ways, and the C code style of the colorful interface library
- [literature reading] an investigation on hardware aware vision transformer scaling
- 【C语言】经典的递归问题
- Galaxy securities online account opening commission, is online account opening safe for customer managers
- Geek challenge 2019 (review the loopholes)
- Part II - C language improvement_ 7. Structure
- When aw9523b chip is used to drive 16 channel led, the LED is wrongly lit
- 带你熟悉云网络的“电话簿”:DNS
- Thousands of tiles' tilt model browsing speeds up, saying goodbye to the embarrassment of jumping out one by one
- Relationship between Unicode and UTF-8
猜你喜欢

The place where the dream begins ---- first knowing C language (2)

4-4 object lifecycle

Dajiang Zhitu and CC have produced multiple copies of data. How to combine them into one and load them in the new earth map

文件上传到服务器

文件上传到OSS文件服务器

会议OA之我的会议

Meeting OA my meeting

ES6新特性

MVC三层架构

Mysql database complex operations: Database Constraints, query / connect table operations
随机推荐
What are the use cases in the Internet of things industry in 2022?
The attorney general and the director of the national security service of Ukraine were dismissed
[interview: concurrency 26: multithreading: two-phase termination mode] volatile version
Part II - C language improvement_ 6. Multidimensional array
带你熟悉云网络的“电话簿”:DNS
华测RTK采集的GPX数据如何带属性转出kml、shp进行后续的管理和分析
Part II - C language improvement_ 7. Structure
Embedded system migration [8] - device tree and root file system migration
【不积跬步无以至千里】统计日志指定时间段内的关键词
2022.7.18-----leetcode.749
08 design of intelligent agricultural environmental monitoring system based on ZigBee
08_ Event modifier
Paging plug-in -- PageHelper
The difference between SQL join and related subinquiry
Chapter 3 cross domain issues
Chapter 1 Introduction and use skills of interceptors
The NFT market pattern has not changed. Can okaleido set off a new round of waves?
上千Tile的倾斜模型浏览提速,告别一块一块往外蹦的尴尬
【面试:并发篇27:多线程:犹豫模式】
Part II - C language improvement_ 5. Bit operation