PAT-CCCC练习:L2-006.树的遍历

本文介绍了一种通过给定的后序遍历和中序遍历序列来构建二叉树,并最终输出该树的层序遍历序列的方法。通过递归算法构建树结构,并使用队列实现层序遍历。

L2-006. 树的遍历

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。

输入格式:

输入第一行给出一个正整数N(<=30),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。

输出格式:

在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:

72 3 1 5 7 6 41 2 3 4 5 6 7

输出样例:

4 1 6 3 5 7 2


记录于此,作为后序中序制作树的复习。

#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>

using namespace std;

struct node
{
	int l,r;
}tree[35];
int back[35],mid[35];

int build(int midl,int midr,int backl,int backr)//中序左右,后序左右 
{
	if(midl>midr)
		return 0;//尽头处左右子树设为0 
	
	int root=backr;
	int ptr;
	for(ptr=midl;mid[ptr]!=back[backr];ptr++);
	
	int len=ptr-midl;
	
	tree[root].l=build(midl,ptr-1,backl,backl+len-1);
	tree[root].r=build(ptr+1,midr,backl+len,backr-1);
	
	return root;
}
void fgo(int root)//层序遍历 
{
	queue<node>q;
	vector<int>v;
	
	q.push(tree[root]);
	v.push_back(root); 
	while(!q.empty())
	{
		node t=q.front();
		if(t.l!=0)
		{
			q.push(tree[t.l]);
			v.push_back(t.l);
		}	
		if(t.r!=0)
		{
			q.push(tree[t.r]);
			v.push_back(t.r); 
		}
		q.pop();
	} 
	
	int temp=v.size();
	for(int i=0;i<temp;i++)
	{
		printf("%d%c",back[v[i]],i==temp-1?'\n':' ');
	}
}
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&back[i]);//后序遍历 
	for(int i=1;i<=n;i++)
		scanf("%d",&mid[i]);//中序遍历 
		
	build(1,n,1,n);
	
	fgo(n);
	
	return 0;
}

21:52:18.428 [Thread-0] DEBUG org.springframework.boot.devtools.restart.classloader.RestartClassLoader - Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@3410cfae . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.7.0) 2025-06-11 21:52:19.426 INFO [,,] 23176 --- [ restartedMain] com.hospital.LxjApplication : Starting LxjApplication using Java 19 on DESKTOP-1QAH2DU with PID 23176 (E:\2025年文件\医疗系统\hospital_infront\target\classes started by dell in E:\2025年文件\医疗系统\hospital_infront) 2025-06-11 21:52:19.427 INFO [,,] 23176 --- [ restartedMain] com.hospital.LxjApplication : No active profile set, falling back to 1 default profile: "default" 2025-06-11 21:52:19.473 INFO [,,] 23176 --- [ restartedMain] o.s.c.c.c.ConfigServerConfigDataLoader : Fetching config from server at : http://localhost:8888 2025-06-11 21:52:19.473 INFO [,,] 23176 --- [ restartedMain] o.s.c.c.c.ConfigServerConfigDataLoader : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available 2025-06-11 21:52:19.473 WARN [,,] 23176 --- [ restartedMain] o.s.c.c.c.ConfigServerConfigDataLoader : Could not locate PropertySource ([ConfigServerConfigDataResource@2941e75 uris = array<String>['http://localhost:8888'], optional = true, profiles = list['default']]): I/O error on GET request for "http://localhost:8888/application/default": Connection refused: no further information; nested exception is java.net.ConnectException: Connection refused: no further information 2025-06-11 21:52:19.474 INFO [,,] 23176 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2025-06-11 21:52:19.474 INFO [,,] 23176 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' 2025-06-11 21:52:20.168 INFO [,,] 23176 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2025-06-11 21:52:20.344 INFO [,,] 23176 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 169 ms. Found 10 JPA repository interfaces. 2025-06-11 21:52:20.934 INFO [,,] 23176 --- [ restartedMain] o.s.cloud.context.scope.GenericScope : BeanFactory id=0d55cccc-710b-3b96-99af-97eae531b364 2025-06-11 21:52:21.751 INFO [,,] 23176 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2025-06-11 21:52:21.761 INFO [,,] 23176 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2025-06-11 21:52:21.761 INFO [,,] 23176 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.63] 2025-06-11 21:52:21.850 INFO [,,] 23176 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2025-06-11 21:52:21.851 INFO [,,] 23176 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2376 ms 2025-06-11 21:52:22.211 INFO [,,] 23176 --- [ restartedMain] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource 2025-06-11 21:52:22.388 INFO [,,] 23176 --- [ restartedMain] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited 2025-06-11 21:52:22.662 ERROR [,,] 23176 --- [eate-1539298006] com.alibaba.druid.pool.DruidDataSource : create connection SQLException, url: jdbc:mysql://localhost:3306/hosp_infront?serverTimezone=UTC&&characterEncoding=utf-8, errorCode 1049, state 42000 java.sql.SQLSyntaxErrorException: Unknown database 'hosp_infront' at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.28.jar:8.0.28] at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.28.jar:8.0.28] at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:829) ~[mysql-connector-java-8.0.28.jar:8.0.28] at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:449) ~[mysql-connector-java-8.0.28.jar:8.0.28] at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:242) ~[mysql-connector-java-8.0.28.jar:8.0.28] at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-java-8.0.28.jar:8.0.28] at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:149) ~[druid-1.1.9.jar:1.1.9] at com.alibaba.druid.filter.stat.StatFilter.connection_connect(StatFilter.java:218) ~[druid-1.1.9.jar:1.1.9] at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:143) ~[druid-1.1.9.jar:1.1.9] at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1515) ~[druid-1.1.9.jar:1.1.9] at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1578) ~[druid-1.1.9.jar:1.1.9] at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2466) ~[druid-1.1.9.jar:1.1.9] 2025-06-11 21:52:22.677 ERROR [,,] 23176 --- [eate-1539298006] com.alibaba.druid.pool.DruidDataSource : create connection SQLException, url: jdbc:mysql://localhost:3306/hosp_infront?serverTimezone=UTC&&characterEncoding=utf-8, errorCode 1049, state 42000 java.sql.SQLSyntaxErrorException: Unknown database 'hosp_infront'
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值