当前位置:网站首页>Pat grade a a1086 tree traversals again
Pat grade a a1086 tree traversals again
2022-07-18 04:31:00 【Zhemu】
The question : Stack in and out (push,pop) To determine the unique binary tree , One of the sequences is known to be middle order traversal , To find the subsequent traversal .
analysis :
From the meaning of the question , In a certain order of entering and leaving the stack , Two sequences can be determined , therefore , It should be that the stack entry is a traversal sequence , Stack is also a traversal sequence , Because the title gives ,"An inorder binary tree traversal", therefore , There is a sequence that should be a middle order traversal sequence , And preorder traversal is to visit the root node first , And the nature of the stack sequence process , You can first think of the stack order as a sequence of preorder traversal , Thus, the unique binary tree can be determined , By outputting samples, it can be found that the post order traversal sequence is consistent with the post order traversal sequence of the binary tree , Therefore, it can be concluded that the stacking order is the preorder traversal sequence , Out of the stack is a middle order traversal sequence
The tree is shown in the figure :
Ideas : Set of templates ;
Code :
#include<iostream>
#include<stack>
using namespace std;
const int maxn=50;
int n;
int in[maxn],pre[maxn];
int preindex=0,inindex=0;
struct node{
int data;
node* lchild;
node* rchild;
};
node* create(int preL,int preR,int inL,int inR){
if(preL>preR)return NULL;
node* root=new node;
root->data=pre[preL];
int k=inL;
for(k=inL;k<=inR;k++){
if(in[k]==root->data)break;
}
int leftnum=k-inL;
root->lchild=create(preL+1,preL+leftnum,inL,k-1);
root->rchild=create(preL+leftnum+1,preR,k+1,inR);
return root;
}
int m=0;
void postorder(node* root){// After the sequence traversal -- Left and right ;
if(root==NULL)return ;
postorder(root->lchild);
postorder(root->rchild);
printf("%d",root->data);
m++;
if(m<n)printf(" ");
}
int main( ){
scanf("%d",&n);// Number of nodes ;
getchar( );
stack<int > s;
string str;
int num;
for(int i=0;i<2*n;i++){// Out of the stack , So it is 2n
cin>>str;// Out-of-service scanf Input string type , If you use printf Output string Type to use .c_string transformation ;
if(str=="Push"){
scanf("%d",&num);
s.push(num);
pre[preindex++]=num;
}else {
in[inindex++]=s.top();// The stack takes out the top element with top;
s.pop( );
}
}
node* root=create(0,n-1,0,n-1);
postorder(root);
return 0;
}
Be careful : Do not use scanf Input string type , Want to use printf Output string Type to use str.c_str() function .
边栏推荐
- 动圈式扬声器过载过程
- Open source data quality solution -- Apache Griffin primer
- ThreadLocal原理及源码解析(一步一步点进去,不要背了,学思想)
- 工业以太网交换机的产品性能有哪些呢?
- JS image clipping and compression integration plug-in
- 如何定制.NET6.0的日志记录
- 初识ESP8266(一)————接入点与无线终端模式
- PAT 甲级 A 1099 Build A Binary Search Tree
- 数据湖(十七):Flink与Iceberg整合DataStream API操作
- js看图猜水果蔬菜小游戏代码
猜你喜欢
随机推荐
StarRocks 社区架构出炉,等你通关升级!
JS image editor plug-in filerobot
手机怎样买股票开户流程 开户安全吗
LM-Nav:基于语言、视觉和行为大模型的机器人导航方法
【第二十四题】逻辑闭环(北理工/北京理工大学/程序设计方法与实践/小学期 )
The practice of opengauss database on CentOS, configuration
Go语学习笔记 - 互斥锁 从零开始Go语言
Advanced architects, 16 common principles of microservice design and Governance
#yyds干货盘点# 解决名企真题:循环数比较
解决运行Mongodb报错 Attempted to create a lock file on a read-only directory: /data/db
JVM内存模型——运行时数据区的特点和作用
Class的生命周期
笔记本电脑能连接WiFi但浏览器无法打开网页的解决办法
【c语言】高精度加减乘除模板
JVM垃圾收集之——怎样判定一个对象是不是垃圾
js图片编辑器插件Filerobot
源码中的modCount是什么?有什么作用
Why are the prices of industrial switches high and low?
语句和表达式有什么不同
每日刷题记录 (二十四)






![[entrer dans le cœur de go]](/img/4a/0c287557da803200efe580611e1e8d.png)

