当前位置:网站首页>DS binary tree - parent and child nodes of binary tree
DS binary tree - parent and child nodes of binary tree
2022-07-24 14:47:00 【Running star dailu】
Title Description
The logical structure of a given binary tree is shown in the figure below ,( The result of preorder traversal , Empty tree character ‘0’ Express , for example AB0C00D00), The binary chain storage structure of the binary tree is established .
Write a program to output all leaf nodes and their parent nodes of the tree
Input
Enter an integer in the first line t, Express t A binary tree
From the second line , According to the input method represented by the title , Enter the pre order traversal of each binary tree , Continuous input t That's ok
Output
The first line is traversed in order , Output No 1 Leaf node of an example
The second line outputs 1 In the first example, the parent node corresponding to the leaf
And so on, output the results of other examples
The sample input
3
AB0C00D00
AB00C00
ABCD0000EF000
Sample output
C D
B A
B C
A A
D F
C E Code
#include<bits/stdc++.h>
using namespace std;
int t;
string s,ans;
struct NODE{
char data;
NODE *l,*r;
NODE (){
l=NULL;
r=NULL;
}
};
int id=0,n;
void build(NODE *&head){
if(id>=n || s[id]=='0'){
head=NULL;
id++;
return ;
}
head=new NODE;
head->data=s[id];
id++;
build(head->l);
build(head->r);
}
void dfs(NODE *node,NODE *fa){
if(node->l==NULL && node->r==NULL){
cout<<node->data<<" ";
ans=ans+fa->data+" ";
return ;
}
if(node->l){
dfs(node->l,node);
}
if(node->r){
dfs(node->r,node);
}
}
int main(){
cin>>t;
while(t--){
cin>>s;
n=s.length();
NODE *head=new NODE;
id=0;
ans="";
build(head);
dfs(head,head);
cout<<endl<<ans<<endl;
}
return 0;
}边栏推荐
- REST风格
- Number of bytes occupied by variables of type char short int in memory
- pytorch with torch.no_grad
- Operation of MySQL Library
- VSCode如何调试Nodejs
- Spark Learning Notes (III) -- basic knowledge of spark core
- 电赛设计报告模板及历年资源
- Overview of dobesie wavelet (DB wavelet function) in wavelet transform
- Unity 使用NVIDIA FleX for Unity插件实现制作软体、水流流体、布料等效果学习教程
- 多数据源配置下,解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
猜你喜欢

Conflict resolution of onblur and onchange

The sliding window of Li Kou "step by step" (209. The smallest sub array, 904. Fruit baskets)

How to set packet capturing mobile terminal
![Property datasource is required exception handling [idea]](/img/f3/50d36ed9445695c02e2bd622109d66.png)
Property datasource is required exception handling [idea]

Simple understanding and implementation of unity delegate
![[oauth2] III. interpretation of oauth2 configuration](/img/31/90c79dbc91ee15c353ec46544c8efa.png)
[oauth2] III. interpretation of oauth2 configuration

电赛设计报告模板及历年资源

Learning rate adjustment strategy in deep learning (1)

Grpc middleware implements grpc call retry

Mysql库的操作
随机推荐
Rasa 3.x 学习系列-Rasa FallbackClassifier源码学习笔记
“00后”来了!数睿数据迎来新生代「无代码」生力军
VS编译后的应用缺少dll
Atcoder beginer contest 261e / / bitwise thinking + DP
onBlur和onChange冲突解决方法
多数据源配置下,解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题
Spark: get the access volume of each time period in the log (entry level - simple implementation)
Research Summary / programming FAQs
The first n rows sorted after dataframe grouping nlargest argmax idmax tail!!!!
Production environment tidb cluster capacity reduction tikv operation steps
ISPRS2018/云检测:Cloud/shadow detection based on spectral indices for multi/hyp基于光谱指数的多/高光谱光学遥感成像仪云/影检测
Rasa 3.x 学习系列-Rasa [3.2.3] - 2022-07-18 新版本发布
threw exception [Circular view path [index]: would dispatch back to the current handler URL [/index]
[NLP] next stop, embossed AI
Su Chunyuan, founder of science and technology · CEO of Guanyuan data: making business use is the key to the Bi industry to push down the wall of penetration
股票开户之后就可以购买6%的理财产品了?
Error when using Fiddler hook: 502 Fiddler - connection failed
IEEE Transaction期刊模板使用注意事项
Centos7 installs Damon stand-alone database
[oauth2] III. interpretation of oauth2 configuration