当前位置:网站首页>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
边栏推荐
- tp6阿裏雲短信 window 報 cURL error 60: SSL certificate problem: unable to get local issuer certificate
- Micro build low code zero foundation introductory course
- 今天终于会写System.out.println()了
- Batch multiple files into one hex
- Unity editor hierarchy drop-down menu extension
- Why is one plus one equal to two
- Under the pressure of sales, domestic mobile phones began to reduce prices, but they haven't put down their final face
- Shardingsphere introduction and sub table usage
- VMware virtual machine installation openwrt as side route single arm route img image to vmdk
- Is it better to use a physical machine or a virtual machine to build a website?
猜你喜欢

Some tips for using proxy IP.

What is a dial-up server and what is its use?

What is a proxy IP pool and how to build it?
Unicorn bio raised $3.2 million to turn prototype equipment used to grow meat into commercial products

How to call out services in idea and display the startup class in services

使用代理IP是需要注意什么?

leetcode:27. 移除元素【count remove小操作】

How to choose a good dial-up server?

World Book Day 𞓜 a good book that technicians should not miss (it cutting-edge technology)
![App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]](/img/86/77b67fd28d2583e12f397430a9a62a.png)
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
随机推荐
Use Xdebug breakpoint debugging in postman
Kubernetes cluster installation based on Kirin SP10 server version
What is a dial-up server and what is its use?
MySQL active / standby configuration binary log problem
Is it better to use a physical machine or a virtual machine to build a website?
Is the sinking coffee industry a false prosperity or the eve of a broken situation?
世界读书日 | 技术人不要错过的好书(IT前沿技术)
Use of push() and pop()
What is an API interface?
VMware virtual machine installation openwrt as side route single arm route img image to vmdk
如何对代理IP进行分类?
Wechat public platform test number application, authorized login function and single sign on using hbuilder X and wechat developer tools
How to classify proxy IP?
What is a makefile file?
What is a proxy IP pool and how to build it?
每日一题(2022-04-22)——旋转函数
Chinese scientists reveal a new mechanism for breaking through the bottleneck of rice yield
Time. In ANSI standard library H header file
Campus transfer second-hand market source code
2018 China Collegiate Programming Contest - Guilin Site J. stone game