当前位置:网站首页>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
边栏推荐
猜你喜欢

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

Is the availability of proxy IP equal to the efficiency of proxy IP?

Shardingsphere read write separation

What business scenarios will the BGP server be used in?

World Book Day 𞓜 a good book that technicians should not miss (it cutting-edge technology)

Dynamic batch processing and static batch processing of unity

小程序 canvas 画布半圆环

What are the test steps of dynamic proxy IP?
![NPM yarn startup error [resolved]](/img/8a/8351c33e30da8e08ac0dcb7c7c868e.png)
NPM yarn startup error [resolved]

Lane cross domain problem
随机推荐
What should I pay attention to when using proxy IP?
浅析静态代理ip的三大作用。
Campus transfer second-hand market source code
BGP服务器在什么业务场景会被用到?
Communication summary between MCU and 4G module (EC20)
arduino esp8266 网络升级 OTA
Introduction to micro build low code zero Foundation (lesson 2)
Leetcode46 Full Permutation
012_ Access denied for user ‘root‘@‘localhost‘ (using password: YES)
什么是api接口?
tp6阿里云短信 window 报 cURL error 60: SSL certificate problem: unable to get local issuer certificate
011_RedisTemplate操作Hash
【dpdk】10. Dpdk DNS learning notes
Dynamic memory management
【汇编语言】从最底层的角度理解“堆栈”
Analyze the advantages and disadvantages of tunnel proxy IP.
2018 China Collegiate Programming Contest - Guilin Site J. stone game
What are the common proxy IP problems?
LeetCode 283. Move zero (simple, array) Day12
Lane cross domain problem