当前位置:网站首页>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;
}边栏推荐
- Learning rate adjustment strategy in deep learning (1)
- Activate the newly installed Anaconda in the server
- JS judge whether the data is empty
- 深度学习中的学习率调整策略(1)
- [oauth2] IV. oauth2authorizationrequestredirectfilter
- Rasa 3.x learning series -rasa fallbackclassifier source code learning notes
- VSCode如何调试Nodejs
- 清除字符串中所有空格
- Dialog manager Chapter 2: create frame window
- C operator priority memory formula
猜你喜欢

Maotai ice cream "bucked the trend" and became popular, but its cross-border meaning was not "selling ice cream"

Dialog manager Chapter 2: create frame window

Detailed explanation of address bus, data bus and control bus

Simple understanding and implementation of unity delegate

Conflict resolution of onblur and onchange

使用 Fiddler Hook 报错:502 Fiddler - Connection Failed

多数据源配置下,解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)问题

Number of bytes occupied by variables of type char short int in memory

Tiger mouth waterfall: Tongliang version of xiaohukou waterfall

Leetcode high frequency question 56. merge intervals, merge overlapping intervals into one interval, including all intervals
随机推荐
Under multi data source configuration, solve org.apache.ibatis.binding Bindingexception: invalid bound statement (not found) problem
Centos7 installs Damon stand-alone database
Conversion of timestamp and time in Excel
CSDN垃圾的没有底线!
2022年IAA行业品类发展洞察系列报告·第二期
Atcoder beginer contest 261e / / bitwise thinking + DP
Research Summary / programming FAQs
[oauth2] II. Known changes in oauth2.1
Kotlin类与继承
Caffe framework and production data source for deep learning
Class loading mechanism and parental delegation mechanism
AtCoder Beginner Contest 261 F // 树状数组
VSCode如何调试Nodejs
PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
Rasa 3.x 学习系列-Rasa FallbackClassifier源码学习笔记
Unity uses NVIDIA flex for unity plug-in to realize the effects of making software, water, fluid, cloth, etc. learning tutorial
pytorch with torch.no_ grad
The sliding window of Li Kou "step by step" (209. The smallest sub array, 904. Fruit baskets)
C language -- program environment and preprocessing
Can you buy 6% of financial products after opening a stock account?