当前位置:网站首页>310. minimum height tree
310. minimum height tree
2022-06-27 05:55:00 【Mr Gao】
310. Minimum height tree
A tree is an undirected graph , Any two of these vertices are connected by only one path . let me put it another way , Any connected graph without simple loops is a tree .
Give you a tree that contains n Tree of nodes , Marked as 0 To n - 1 . Given number n And one with n - 1 An undirected edge edges list ( Each side is a pair of labels ), among edges[i] = [ai, bi] Represents the node in the tree ai and bi There is an undirected edge between .
You can select any node in the tree as the root . When selecting a node x As the root node , Let the height of the result tree be h . In all possible trees , Tree with minimum height ( namely ,min(h)) go by the name of Minimum height tree .
Please find all Minimum height tree And press In any order Return a list of their root node labels .
Treelike Height It refers to the number of edges on the longest downward path between the root node and the leaf node .
Example 1:
Input :n = 4, edges = [[1,0],[1,2],[1,3]]
Output :[1]
explain : As shown in the figure , When the root is labeled 1 The nodes of , The height of the tree is 1 , This is the only minimum height tree .
Example 2:
Input :n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
Output :[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;
}
边栏推荐
- 软件测试年终总结报告模板
- Webrtc series - Nomination and ice of 7-ice supplement for network transmission_ Model
- Implementation of easyexcel's function of merging cells with the same content and dynamic title
- Ad22 Gerber files Click to open the Gerber step interface. Official solutions to problems
- [nips 2017] pointnet++: deep feature learning of point set in metric space
- 【Cocos Creator 3.5.1】event.getButton()的使用
- 【FPGA】基于bt1120时序设计实现棋盘格横纵向灰阶图数据输出
- Neo4j database export
- 【QT小作】使用结构体数据生成读写配置文件代码
- Two position relay hjws-9440
猜你喜欢
![Senior [Software Test Engineer] learning route and necessary knowledge points](/img/51/1be2e0812a6bca9e5e8d14bf254254.png)
Senior [Software Test Engineer] learning route and necessary knowledge points

Qt使用Valgrind分析内存泄漏

Asp. Net core6 websocket simple case

How JQ gets the reciprocal elements

KubeSphere 集群配置 NFS 存储解决方案-收藏版
![[FPGA] design and implementation of frequency division and doubling based on FPGA](/img/84/75d473d3d8e670260ba16d06705c2f.png)
[FPGA] design and implementation of frequency division and doubling based on FPGA

免费的 SSH 和 Telnet 客户端PuTTY

Codeforces Round #802 (Div. 2)

Small program of C language practice (consolidate and deepen the understanding of knowledge points)

Asp.Net Core6 WebSocket 简单案例
随机推荐
Ad22 Gerber files Click to open the Gerber step interface. Official solutions to problems
Code is data
双位置继电器DLS-34A DC0.5A 220VDC
DAST black box vulnerability scanner part 6: operation (final)
汇编语言-王爽 第13章 int指令-笔记
Epics record reference 5 -- array analog input recordarray analog input (AAI)
【Cocos Creator 3.5.1】input. Use of on
Spark 之 WholeStageCodegen
JS to implement bidirectional data binding
WebRTC系列-网络传输之7-ICE补充之提名(nomination)与ICE_Model
My opinion on test team construction
openstack实例重启状态就会变成错误处理方法,容器搭建的openstack重启计算节点compute服务方法,开机提示Give root password for maintenance处理方法
Wechat applet refreshes the current page
Jump details of item -h5 list, and realize the function of not refreshing when backing up, and refreshing when modifying data (record scroll bar)
函数栈帧的形成与释放
[collection] Introduction to basic knowledge of point cloud and functions of point cloud catalyst software
洛谷P4683 [IOI2008] Type Printer 题解
Two position relay rxmvb2 r251 204 110dc
Open the door small example to learn ten use case diagrams
Unity中跨平台获取系统音量