当前位置:网站首页>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;
}
边栏推荐
- 力扣152题:乘积最大子数组
- Meeting OA my meeting
- Qunar travel massive indicator data collection and storage
- Mysql database complex operations: Database Constraints, query / connect table operations
- What is Tencent cloud lightweight application server? What are the differences between CVM and ECS?
- 带你熟悉云网络的“电话簿”:DNS
- C语言数组
- Chapter 1 Introduction and use skills of interceptors
- Simple SQL optimization
- Positioning of soaring problems caused by online MySQL CPU
猜你喜欢

Hcip day 2_ HCIA review comprehensive experiment

What scenarios are Tencent cloud lightweight application servers suitable for?

Everything you should know about wearable NFT!

The basic operation of data tables in MySQL is very difficult. This experiment will take you through it from the beginning

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

08 design of intelligent agricultural environmental monitoring system based on ZigBee

第二部分—C语言提高篇_6. 多维数组

Meeting OA my meeting

Pytorch learning record (II): tensor

Share a regular expression
随机推荐
Familiarize you with the "phone book" of cloud network: DNS
Pytorch learning record (II): tensor
Embedded system migration [8] - device tree and root file system migration
15_ Key function and principle
Azure synapse analytics Performance Optimization Guide (4) -- optimize performance using result set caching
Share a regular expression
[literature reading] an investigation on hardware aware vision transformer scaling
Number that cannot be bought
13_ conditional rendering
Geek challenge 2019 (review the loopholes)
Recent answers - column
push to origin/master was rejected 错误解决方法
Complex SQL_ 01
【C语言】数组
What are the use cases in the Internet of things industry in 2022?
Add an article ----- scanf usage
大疆智图、CC生产了多份数据,如何合并为一份在图新地球进行加载
In depth interpretation of the investment logic of the consortium's participation in the privatization of Twitter
18、打开、保存文件对话框使用小记
Dajiang Zhitu and CC have produced multiple copies of data. How to combine them into one and load them in the new earth map