当前位置:网站首页>DS binary tree - maximum distance of binary tree nodes
DS binary tree - maximum distance of binary tree nodes
2022-07-24 14:49:00 【Running star dailu】
Title Description
The distance between two nodes of a binary tree is that one node passes through the parent node , The number of branches that an intermediate node such as an ancestor node passes through to reach another node . The maximum distance of a binary tree node is the maximum distance between all nodes . for example , The maximum distance between nodes of the binary tree shown in the figure below is 3,C and D Distance of .
A binary tree is created in a preordered traversal order ,# Empty tree . Calculate the two nodes of the maximum distance and the maximum distance of the binary tree node ( Suppose that the two nodes with the largest distance in the binary tree are unique ).

Input
Number of tests T
The first 2 After that T That's ok , Each line traverses the result of a binary tree first (# Empty tree )
Output
For every binary tree , The maximum distance between the nodes of the output tree and the nodes with the maximum distance , See the example for the output format .
The sample input
3
A##
ABC##EF#G###D##
ABEH###F#K###
Sample output
0:
5:G D
4:H KCode
#include "bits/stdc++.h"
using namespace std;
const int maxn=1e5+20;
struct NODE{
char data;
int d,d2;
NODE *l,*r,*fa;
NODE(){
l=NULL;
r=NULL;
fa=NULL;
d=0;
}
};
int t,n;
string s;
int maxx;
NODE *qwe=new NODE;
NODE *node=new NODE;
int id=0;
void build(NODE *&head,NODE *fa){
if(id>=n || s[id]=='#'){
head=NULL;
id++;
return ;
}
head=new NODE;
head->data=s[id];
head->fa=fa;
head->d=fa->d+1;
if(head->d>maxx){
maxx=head->d;
qwe=head;
}
id++;
build(head->l,head);
build(head->r,head);
}
bool check(NODE *a,NODE *b){
queue<NODE *> qq;
if(a->l) qq.push(a->l);
while(!qq.empty()){
auto now=qq.front();
qq.pop();
if(now->data==b->data) return false;
if(now->l) qq.push(now->l);
if(now->r) qq.push(now->r);
}
return true;
}
void find(NODE *head){
queue<NODE*> q;
map<char,int> mp;
q.push(qwe);
mp[qwe->data]++;
qwe->d2=0;
int ans=0;
NODE *asd=new NODE;
while(!q.empty()){
auto now=q.front();
q.pop();
if(now->d2>ans){
ans=now->d2;
asd=now;
}
if(now->fa!=node && now->fa!=NULL && mp.count(now->fa->data)==0){
now->fa->d2=now->d2+1;
q.push(now->fa);
mp[now->fa->data]++;
}
if(now->l!=node &&now->l && mp.count(now->l->data)==0){
now->l->d2=now->d2+1;
q.push(now->l);
mp[now->l->data]++;
}
if(now->r!=node &&now->r && mp.count(now->r->data)==0){
now->r->d2=now->d2+1;
q.push(now->r);
mp[now->r->data]++;
}
}
cout<<ans<<":";
if(ans!=0){
if(head->l==NULL)
cout<<asd->data<<" "<<qwe->data;
else cout<<qwe->data<<" "<<asd->data;
}
cout<<endl;
}
int main(){
// freopen("123.in","r",stdin);
cin>>t;
while(t--){
cin>>s;
n=s.length();
id=0;
NODE *head;
maxx=0;
build(head,node);
find(head);
// cout<<maxx<<" "<<qwe->data<<endl;
}
return 0;
}边栏推荐
- AtCoder Beginner Contest 261E // 按位思考 + dp
- Conflict resolution of onblur and onchange
- The accuracy of yolov7 in cracking down on counterfeits, not all papers are authentic
- Rest style
- Rasa 3.x learning series -rasa [3.2.4] - 2022-07-21 new release
- threw exception [Circular view path [index]: would dispatch back to the current handler URL [/index]
- Which brokerage has the lowest commission? I want to open an account. Is it safe to open an account on my mobile phone
- 关于构建网络安全知识库方向相关知识的学习和思考
- Performance test - analyze requirements
- Property datasource is required exception handling [idea]
猜你喜欢

Dialog manager Chapter 2: create frame window

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

Leetcode · daily question · 1184. distance between bus stops · simulation

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

打假Yolov7的精度,不是所有的论文都是真实可信

Operation of MySQL Library

Detailed explanation of IO model (easy to understand)
![[oauth2] III. interpretation of oauth2 configuration](/img/31/90c79dbc91ee15c353ec46544c8efa.png)
[oauth2] III. interpretation of oauth2 configuration

2022年IAA行业品类发展洞察系列报告·第二期

Not configured in app.json (uni releases wechat applet)
随机推荐
深入浅出边缘云 | 2. 架构
Tensorflow framework of deep learning realizes vgg/rnn network / verification code generation and recognition / text classification
pip换源
Spark: get the access volume of each time period in the log (entry level - simple implementation)
Grpc middleware implements grpc call retry
Rasa 3.x 学习系列-Rasa [3.2.3] - 2022-07-18 新版本发布
Rasa 3.x learning series -rasa [3.2.4] - 2022-07-21 new release
Class loading mechanism and parental delegation mechanism
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
Unity 使用NVIDIA FleX for Unity插件实现制作软体、水流流体、布料等效果学习教程
DDD based on ABP -- Entity creation and update
After reading this article, I found that my test cases were written in garbage
JS judge whether the data is empty
Video game design report template and resources over the years
"After 00" is coming! Digital data ushers in a new generation of "codeless" forces
基于ABP实现DDD--实体创建和更新
mysql
Bibliometrix: dig out the one worth reading from thousands of papers!
AtCoder Beginner Contest 261E // 按位思考 + dp
Attributeerror: module 'distutils' has no attribute' version error resolution