当前位置:网站首页>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;
}边栏推荐
- 打假Yolov7的精度,不是所有的论文都是真实可信
- 电赛设计报告模板及历年资源
- Data analysis and mining 1
- Extjs4 instance address and Chinese document address
- mysql
- Property datasource is required exception handling [idea]
- Decrypt "sea Lotus" organization (domain control detection and defense)
- Conversion of timestamp and time in Excel
- The vs compiled application is missing DLL
- Under multi data source configuration, solve org.apache.ibatis.binding Bindingexception: invalid bound statement (not found) problem
猜你喜欢

Must use destructuring props assignmenteslint

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

2022 IAA industry category development insight series report - phase II

Jmmert aggregation test report

After reading this article, I found that my test cases were written in garbage

对话框管理器第二章:创建框架窗口
![[NLP] next stop, embossed AI](/img/fc/4997309d0d53c5b6eb441ac39e6929.jpg)
[NLP] next stop, embossed AI

Regular expression and bypass cases

Sword finger offer II 001. integer division

How to set packet capturing mobile terminal
随机推荐
Atcoder beginer contest 261e / / bitwise thinking + DP
2.4. properties of special profile
Mini examination - examination system
spark:获取日志中每个时间段的访问量(入门级-简单实现)
onBlur和onChange冲突解决方法
Automated penetration scanning tool
Self join usage of SQL
Binlog and iptables prevent nmap scanning, xtrabackup full + incremental backup, and the relationship between redlog and binlog
Learning rate adjustment strategy in deep learning (1)
[oauth2] II. Known changes in oauth2.1
threw exception [Circular view path [index]: would dispatch back to the current handler URL [/index]
交换
基于ABP实现DDD--实体创建和更新
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
Ztree tree Metro style mouse through the display user-defined controls add, edit, delete, down, up operations
SQL Server syntax - create database
Kotlin类与继承
Tensorflow framework of deep learning realizes vgg/rnn network / verification code generation and recognition / text classification
CSDN garbage has no bottom line!
Conversion of timestamp and time in Excel