当前位置:网站首页>PTA: Romantic reflection [binary tree reconstruction] [depth first traversal]
PTA: Romantic reflection [binary tree reconstruction] [depth first traversal]
2022-04-23 02:08:00 【JackDawn!?】
subject

sample input :
8
6 8 7 4 5 1 3 2
8 5 4 7 6 3 2 1
sample output :
R: 1 2 3 4 5
L: 1 6 7 8 5
Ideas
Reconstruction of binary tree , Then depth first traversal , If you look at the right view , First traverse the right subtree ; If left view , First traverse the left subtree . During the traversal process, record whether any nodes at each depth have been seen , Print out the first node seen .
Code
#include <cstdio>
#include <cstring>
using namespace std;
struct node {
int n;
node* l, *r;
node() {
l = r = NULL;
}
};
int last[25], mid[25];
node* build(int mb, int me, int lb, int le) {
if(mb > me || lb > le) return NULL;
node* t = new node();
t->n = last[le];
int m;
for(int i = mb; i <= me; ++i) {
if(mid[i] == last[le]) {
m = i;
break;
}
}
int ls = m-mb;
t->l = build(mb, m-1, lb, lb+ls-1);
t->r = build(m+1, me, lb+ls, le-1);
return t;
}
bool vis[20];
void right(node* tree, int depth) {
if(tree) {
if(!vis[depth]) {
printf(" %d", tree->n);
vis[depth] = true;
}
right(tree->r, depth+1);
right(tree->l, depth+1);
}
}
void left(node* tree, int depth) {
if(tree) {
if(!vis[depth]) {
printf(" %d", tree->n);
vis[depth] = true;
}
left(tree->l, depth+1);
left(tree->r, depth+1);
}
}
int main(void)
{
freopen("in.txt", "r", stdin);
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
scanf("%d", &mid[i]);
}
for(int i = 0; i < n; ++i) {
scanf("%d", &last[i]);
}
node* tree = build(0, n-1, 0, n-1);
memset(vis, 0, sizeof(vis));
printf("R:");
right(tree, 0);
printf("\n");
memset(vis, 0, sizeof(vis));
printf("L:");
left(tree, 0);
return 0;
}
版权声明
本文为[JackDawn!?]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230205355219.html
边栏推荐
- Unicorn bio raised $3.2 million to turn prototype equipment used to grow meat into commercial products
- World Book Day 𞓜 a good book that technicians should not miss (it cutting-edge technology)
- PTA: 浪漫倒影 [二叉树重建] [深度优先遍历]
- 一些使用代理IP的小技巧。
- Tp6 Alibaba cloud SMS window reports curl error 60: SSL certificate problem: unable to get local issuer certificate
- Is it better to use a physical machine or a virtual machine to build a website?
- Numerical remapping method (remap)
- Today will finally write system out. Println()
- leetcode:27. 移除元素【count remove小操作】
- Lane cross domain problem
猜你喜欢

VMware virtual machine installation openwrt as side route single arm route img image to vmdk

MySQL C language connection

【Chrome扩展程序】content_script的跨域问题

Network jitter tool clumsy

New book recommendation - IPv6 technology and application (Ruijie version)

How to initialize "naming and surname" in C language

Chinese scientists reveal a new mechanism for breaking through the bottleneck of rice yield

007_Redis_Jedis连接池

How to choose a good dial-up server?

Halo open source project learning (I): project launch
随机推荐
How to write the resume of Software Test Engineer so that HR can see it?
Flink real-time data warehouse project - Design and implementation of DWS layer
What are the test steps of dynamic proxy IP?
Startup of openstack service
每日一题(2022-04-22)——旋转函数
【ValueError: math domain error】
拨号vps会遇到什么问题?
What problems will you encounter when dialing VPS?
89 logistic regression user portrait user response prediction
Summary of I / O knowledge points
arduino esp8266 网络升级 OTA
浅析一下隧道代理IP的优缺点。
一加一为什么等于二
Micro build low code zero foundation introductory course
从0开始开发一个chrome插件(2)
Numerical remapping method (remap)
用TensorFlow实现线性回归(包括过程中出现的问题及解决方法)
009_Redis_RedisTemplate入门
2018 China Collegiate Programming Contest - Guilin Site J. stone game
代理IP可用率是不是等同于代理IP的效率?