当前位置: 首页 > news >正文

天梯赛 L2-011 玩转二叉树

和上面一道树的题思路一样,只需要换一下递归的顺序即可。

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
// #define int long long
typedef long long ll;
const int N = 50;
int n;
int a[N],b[N];
struct nod{
	int value;
	nod* l = NULL;
	nod* r = NULL;
};
nod* build(int al,int ar,int bl,int br){
	if(al > ar) return NULL;
	nod* root = (nod*)malloc(sizeof(nod));
	root->value = a[al];
	int x = a[al];
	int p = bl;
	while(b[p] != x){
		p++;
	}
	int len = p - bl;
	root->l = build(al+1,al+len,bl,p-1);
	root->r = build(al+len+1,ar,p+1,br);
	return root;
}
void bfs(nod* x){
	queue<nod> q;
	q.push(*x);
	while(!q.empty()){
		nod tmp = q.front();
		q.pop();
		if(tmp.value != x->value) cout<<" ";
		cout<<tmp.value;
		if(tmp.r != NULL) q.push(*(tmp.r));
		if(tmp.l != NULL) q.push(*(tmp.l));
	}
}
void solve() {
	cin>>n;
	for(int i = 1 ; i <= n ; i++){
		cin>>b[i];
	}
	for(int i = 1 ; i <= n ; i++){
		cin>>a[i];
	}
	nod* head = build(1,n,1,n);
	bfs(head);	//输出
}
int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tt = 1;
    // cin >> tt;
    while (tt--) {
        solve();
    }
    return 0;
}

http://www.dtcms.com/a/80039.html

相关文章:

  • 使用uniapp的vite版本进行微信小程序开发,在项目中使用mqtt连接、订阅、发布信息
  • 若依(RuoYi)框架新手使用指南
  • Bilve 搭建手册
  • L2TP的LAC拨号模式实验
  • 【SpringBoot】你不能不会的SpringBoot图形验证码生成
  • 自学Python创建强大AI:从入门到实现DeepSeek级别的AI
  • bootstrap介绍(前端框架)(提供超过40种可复用组件,从导航栏到轮播图,从卡片到弹窗)bootstrap框架
  • 1688商品数据实战:API搜索接口开发与供应链分析应用
  • Linux--进程创建
  • CTF类题目复现总结-[WUSTCTF2020]girlfriend 1
  • wpa_supplicant驱动初始化源码分析
  • Gin框架学习
  • 【sgFloatDialog】自定义组件:浮动弹窗,支持修改尺寸、拖拽位置、最大化、还原、最小化、复位
  • Vue3 在组件中判断事件是否注册
  • js原型链与自动装箱机制
  • 从OSI七层网络模型角度了解CAN通信协议
  • 关于金融开发领域的一些专业知识总结
  • jmeter接口测试[-面试篇-]
  • 【YOLOv8改进 - C2f融合】C2f融合SCConv :即插即用的空间和通道重建卷积
  • 我的uniapp自定义模板
  • 基于SpringBoot + Vue 的药店药品信息管理系统
  • Yolo v4 (Darknet) Mac M2 安装与运行
  • kmp算法的实现
  • 测试专项3:算法测试基础理论速查手册
  • Spring Boot 整合 Apache Flink 教程
  • 二. JAVA数据类型与变量
  • 软考中级-软件设计师 准备
  • OpenWrt开发第4篇:设置开发板的IP-基于Raspberry Pi 4B开发板
  • 2025-03-20 学习记录--C/C++-C 库函数 - toupper()、tolower()、 isspace()
  • Python(冒泡排序、选择排序、插入法排序、快速排序,算法稳定性)