当前位置:网站首页>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;
}边栏推荐
- ISPRS2018/云检测:Cloud/shadow detection based on spectral indices for multi/hyp基于光谱指数的多/高光谱光学遥感成像仪云/影检测
- PrestoUserError: PrestoUserError(type=USER_ERROR, name=INVALID_FUNCTION_ARGUMENT, message=“Escape st
- PCA of [machine learning]
- Conversion of timestamp and time in Excel
- Video game design report template and resources over the years
- "After 00" is coming! Digital data ushers in a new generation of "codeless" forces
- Data analysis and mining 2
- Google Earth Engine——使用MODIS数据进行逐月数据的过火(火灾)面积并导出
- Conflict resolution of onblur and onchange
- Decrypt "sea Lotus" organization (domain control detection and defense)
猜你喜欢

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

北京一卡通以35288.8529万元挂牌出让68.45%股权,溢价率为84%

Detailed explanation of address bus, data bus and control bus

Centos7 installs Damon stand-alone database

Video game design report template and resources over the years

TypeError: Cannot read property ‘make‘ of undefined

Beijing all in one card listed and sold 68.45% of its equity at 352.888529 million yuan, with a premium rate of 84%

Summary of feature selection: filtered, wrapped, embedded

老虎口瀑布:铜梁版小壶口瀑布

The server switches between different CONDA environments and views various user processes
随机推荐
Mysql库的操作
JS judge whether it is an integer
The accuracy of yolov7 in cracking down on counterfeits, not all papers are authentic
关于构建网络安全知识库方向相关知识的学习和思考
Decrypt "sea Lotus" organization (domain control detection and defense)
The server switches between different CONDA environments and views various user processes
Google Earth Engine——使用MODIS数据进行逐月数据的过火(火灾)面积并导出
Rasa 3.x 学习系列-Rasa FallbackClassifier源码学习笔记
Can you buy 6% of financial products after opening a stock account?
“00后”来了!数睿数据迎来新生代「无代码」生力军
Under multi data source configuration, solve org.apache.ibatis.binding Bindingexception: invalid bound statement (not found) problem
LeetCode·每日一题·1184.公交站间的距离·模拟
dataframe 分组后排序的前n行 nlargest argmax idmax tail !!!!
JS judge whether the data is empty
Kotlin类与继承
Production environment tidb cluster capacity reduction tikv operation steps
pytorch with torch.no_grad
SQL Server syntax - create database
Rasa 3.x learning series -rasa [3.2.3] - new version released on July 18, 2022
异或程序