当前位置:网站首页>DS二叉树—二叉树结点的最大距离
DS二叉树—二叉树结点的最大距离
2022-07-21 16:26:00 【奔跑的星黛露】
题目描述
二叉树两个结点的距离是一个结点经过双亲结点,祖先结点等中间结点到达另一个结点经过的分支数。二叉树结点的最大距离是所有结点间距离的最大值。例如,下图所示二叉树结点最大距离是3,C和D的距离。
二叉树用先序遍历顺序创建,#表示空树。计算二叉树结点最大距离和最大距离的两个结点(假设二叉树中取最大距离的两个结点唯一)。
输入
测试次数T
第2行之后的T行,每行为一棵二叉树先序遍历结果(#表示空树)
输出
对每棵二叉树,输出树的结点最大距离和最大距离的结点,输出格式见样例。
样例输入
3
A##
ABC##EF#G###D##
ABEH###F#K###
样例输出
0:
5:G D
4:H K
代码
#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;
}
边栏推荐
- 如何设计产品MVP实现价值最大化
- 20天能拿下PMP吗?
- Web3 n'a jamais échappé à ces vieux géants.
- How to write SQL statements?
- MLX90640 红外热成像仪测温模块开发笔记(三)
- Architect growth: when it comes to architecture, what is it
- Can I win PMP in 20 days?
- Tapdata 与优炫数据库完成产品兼容性互认证
- Wechat payment native (I) preparation and related knowledge
- Free sharing, easy to use Bates training method to mediate ciliary myopic recovery training
猜你喜欢
Dynamics crm: when removing a control from the form, the prompt "the field you are trying to remove is required by the system or business"“
Development dynamics | stonedb 2022 release milestone
Comment ouvrir plusieurs wechat sur l'ordinateur
How to use document tools for API management?
Dynamics CRM: several situations in which the display fields in a form become read-only
编程学习的资料分享
Notes on the establishment of the official website of the public network (V): the complete process of filing the domain name with the Ministry of industry and information technology and resolving the
Comment changer la police de la console en console?
Dynamics crm: how to monitor and manage workflow processes and view their running history
Read the paper with me - multi model text recognition network
随机推荐
Comment ouvrir plusieurs wechat sur l'ordinateur
Dynamics CRM: Dashboard(仪表板)中设置的实体显示不出来的问题
Dynamics CRM: the entity set in dashboard cannot be displayed
Mutual certification of product compatibility between tapdata and Youxuan database
Fastjson 代码执行 CVE-2022-25845
带你认识8个软件设计中的谬误
Android-第十三节03SQLite数据库详解
国家互联网信息办公室有关负责人就对滴滴全球股份有限公司依法作出网络安全审查相关行政处罚的决定答记者问
易观分析《2022年中国数据安全市场数据监测报告》正式启动
Dynamics crm: connection similar to many to many relationship
pytorch 32 onnx模型每次输出的结果都会变的解决方案
Dynamics CRM: 邮箱配置(二) - 对于Dynamics CRM中收不到邮件的可能原因之一
MySQL sub database and sub table and its smooth expansion scheme
Json文件编辑器
华泰证券开户安全么,不知道怎么操作
leetcode-09(下一个排列+快乐数+全排列)
Dynamics CRM: several situations in which the display fields in a form become read-only
web3再牛 也没能逃出这几个老巨头的手掌心
使用OpenCv+Arduino实现挂机自动打怪
Introduction to single chip microcomputer: light up the first LED light