当前位置:网站首页>310. 最小高度树
310. 最小高度树
2022-06-27 05:40:00 【Mr Gao】
310. 最小高度树
树是一个无向图,其中任何两个顶点只通过一条路径连接。 换句话说,一个任何没有简单环路的连通图都是一棵树。
给你一棵包含 n 个节点的树,标记为 0 到 n - 1 。给定数字 n 和一个有 n - 1 条无向边的 edges 列表(每一个边都是一对标签),其中 edges[i] = [ai, bi] 表示树中节点 ai 和 bi 之间存在一条无向边。
可选择树中任何一个节点作为根。当选择节点 x 作为根节点时,设结果树的高度为 h 。在所有可能的树中,具有最小高度的树(即,min(h))被称为 最小高度树 。
请你找到所有的 最小高度树 并按 任意顺序 返回它们的根节点标签列表。
树的 高度 是指根节点和叶子节点之间最长向下路径上边的数量。
示例 1:
输入:n = 4, edges = [[1,0],[1,2],[1,3]]
输出:[1]
解释:如图所示,当根是标签为 1 的节点时,树的高度是 1 ,这是唯一的最小高度树。
示例 2:
输入:n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
输出:[3,4]
/** * Note: The returned array must be malloced, assume caller calls free(). */
int min_h;
int dfs( int** edges, int edgesSize,int now_node,int h,int *r,int *rs){
int i=0;
int maxh=0;
int num=0;
for(i;i<edgesSize;i++){
if(edges[i][0]==now_node&&r[edges[i][1]]==0){
r[edges[i][1]]=1;
int max1= dfs(edges,edgesSize,edges[i][1],h+1,r,rs);
if(max1>maxh){
maxh=max1;
}
r[edges[i][1]]=0;
}
if(edges[i][1]==now_node&&r[edges[i][0]]==0){
r[edges[i][0]]=1;
int max1= dfs(edges,edgesSize,edges[i][0],h+1,r,rs);
if(max1>maxh){
maxh=max1;
}
r[edges[i][0]]=0;
}
}
if(maxh==0){
return h;
}
else{
return maxh;
}
}
int* findMinHeightTrees(int n, int** edges, int edgesSize, int* edgesColSize, int* returnSize){
int r[n];
int rs[n];
int i;
int *re=(int *)malloc(sizeof(int)*n);
for(i=0;i<n;i++){
r[i]=0;
rs[i]=0;
}
int min=1000;
for(i=0;i<n;i++){
if(rs[i]!=0){
if(rs[i]<min){
min=rs[i];
}
continue;
}
r[i]=1;
int h2=dfs(edges,edgesSize,i,1,r,rs);
rs[i]=h2;
if(h2<min){
min=h2;
}
r[i]=0;
}
printf("min %d ",min);
int size=0;
for(i=0;i<n;i++){
printf("%d| ",rs[i]);
if(rs[i]==min){
re[size++]=i;
}
}
* returnSize=size;
return re;
}
边栏推荐
- 使用域名转发mqtt协议,避坑指南
- 认知篇----2022高考志愿该如何填报
- 快速排序(非遞歸)和歸並排序
- 齐纳二极管 稳压二极管 SOD123封装 正负区分
- 机械转码日记【17】模板,STL简介
- 流媒体协议初探(MPEG2-TS、RTSP、RTP、RTCP、SDP、RTMP、HLS、HDS、HSS、MPEG-DASH)
- NLP-D62-nlp比赛D31&刷题D15
- 体验 win10 下 oceanbase 数据库
- Machunmei, the first edition of principles and applications of database... Compiled final review notes
- Zener diode zener diode sod123 package positive and negative distinction
猜你喜欢

双位置继电器JDP-1440/DC110V

Junda technology - centralized monitoring scheme for multi brand precision air conditioners

ES6 0622 III

Avoid asteroids

Reading graph augmentations to learn graph representations (lg2ar)

leetcode298周赛记录

Two position relay rxmvb2 r251 204 110dc

Quick sort (non recursive) and merge sort

pycharm 如何安装 package

NLP-D62-nlp比赛D31&刷题D15
随机推荐
流媒体协议初探(MPEG2-TS、RTSP、RTP、RTCP、SDP、RTMP、HLS、HDS、HSS、MPEG-DASH)
使用域名转发mqtt协议,避坑指南
Machunmei, the first edition of principles and applications of database... Compiled final review notes
What is BFC? What's the usage?
Gao Xiang slam14 lecture - note 1
stm32读取IO高低电平状态
Wechat applet websocket use case
Open the door small example to learn ten use case diagrams
RTP sending PS stream tool (open source)
IAR Systems全面支持芯驰科技9系列芯片
Webrtc series - Nomination and ice of 7-ice supplement for network transmission_ Model
关于元器件封装的一些文章和一下我的体会
【FPGA】基于bt1120时序设计实现棋盘格横纵向灰阶图数据输出
neo4j community与neo4j desktop冲突
快速排序(非递归)和归并排序
STM32 MCU pin_ How to configure the pin of single chip microcomputer as pull-up input
The most detailed download tutorial of MySQL
快速排序(非遞歸)和歸並排序
微信小程序刷新当前页面
洛谷P2939 [USACO09FEB]Revamping Trails G 题解