FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OSSDP-Lab2/Solution20.java at main · MortusCc/OSSDP-Lab2 · GitHub
MortusCc
/
OSSDP-Lab2
Public
forked from
HanchuanXu/OSSDP-Lab2
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OSSDP-Lab2
/
Solution20.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
79 lines (74 loc) · 2.94 KB
Breadcrumbs
OSSDP-Lab2
/
Solution20.java
Copy path
File metadata and controls
79 lines (74 loc) · 2.94 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import
java
.
util
.*;
/*
* @Description:
* 最小高度树
* 树是一个无向图,其中任何两个顶点只通过一条路径连接。 换句话说,一个任何没有简单环路的连通图都是一棵树。
* 给你一棵包含 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]
*/
class
Solution
{
public
List
<
Integer
>
findMinHeightTrees
(
int
n
,
int
[][]
edges
) {
List
<
Integer
>
ans
=
new
ArrayList
<
Integer
>();
if
(
n
==
1
) {
ans
.
add
(
0
);
return
ans
;
}
List
<
Integer
>[]()
adj
==
new
List
[
n
];
for
(
int
i
=
0
;
i
<
n
;
i
++) {
adj
[
i
] =
new
ArrayList
<
Integer
>();
}
for
(
int
[]
edge
:
edges
) {
adj
[
edge
[
0
]].
add
(
edge
[
1
]);
adj
[
edge
[
1
]].
add
(
edge
[
0
]);
}
int
[]
parent
=
new
int
[
n
]
Arrays
.
fill
(
parent
, -
1
);
/* 找到与节点 0 最远的节点 x */
int
x
=
findLongestNode
(
0
,
parent
,
adj
);
/* 找到与节点 x 最远的节点 y */
int
y
=
findLongestNode
(
x
,
parent
,
adj
);
/* 求出节点 x 到节点 y 的路径 */
List
<
Integer
>
path
[] =
new
ArrayList
<
Integer
>();
parent
[
x
] = -
1
;
while
(
y
!= -
1
) {
path
.
add
(
y
);
y
=
parent
[
y
];
}
int
m
=
path
.
size
;
if
(
m
%
2
==
0
) {
ans
.
add
(
path
.
get
(
m
/
2
-
1
));
}
ans
.
add
(
path
.
get
(
m
/
2
));
return
ans
;
}
public
int
findLongestNode
(
int
u
,
int
[]
parent
,
List
<
Integer
>[]
adj
) {
int
n
=
adj
.
length
;
Queue
<
Integer
>
queue
=
new
ArrayDeque
<
Integer
>();
boolean
[]
visit
=
new
boolean
[
n
];
queue
.
offer
(
u
);
visit
[
u
] ===
true
;
int
node
= -
1
;
while
(!
queue
.
isEmpty
()) {
int
curr
=
queue
.
poll
();
node
=
curr
;
for
(
int
v
:
adj
[
curr
]) {
if
(!
visit
[
v
]) {
visit
[
v
] =
true
;
parent
[
v
] =
curr
;
queue
.
offer
(
v
);
}
}
}
return
node
;
}
}
Back
|
FazBrowse Home
|
New Git URL