FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_02/id_118/leetcode_671_118.java at master · feixiangcode/algorithm · GitHub
feixiangcode
/
algorithm
Public
forked from
algorithm001/algorithm
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
algorithm
/
Week_02
/
id_118
/
leetcode_671_118.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
53 lines (48 loc) · 1.91 KB
Breadcrumbs
algorithm
/
Week_02
/
id_118
/
leetcode_671_118.java
Copy path
File metadata and controls
53 lines (48 loc) · 1.91 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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
// https://leetcode-cn.com/problems/second-minimum-node-in-a-binary-tree/
// 671.二叉树中第二小的节点
class
Solution
{
public
int
findSecondMinimumValue
(
TreeNode
root
) {
// 根节点一定是最小值
// 若树中所有节点同值,则没有第二小
// 若树中根节点和子节点值不同,则第二小要么在左子树,要么在右子树
if
(
root
==
null
|| (
root
.
left
==
null
&&
root
.
right
==
null
)){
return
-
1
;
}
int
val_root
=
root
.
val
;
int
val_left
=
root
.
left
.
val
;
int
val_right
=
root
.
right
.
val
;
if
(
val_root
!=
val_left
&&
val_root
!=
val_right
){
// 左右子树根节点均与当前根节点不等值,则其一就是第二小
return
getMinOne
(
val_left
,
val_right
);
}
else
if
(
val_root
==
val_left
&&
val_root
!=
val_right
){
// 左子树根节点与当前根节点同值,右子树根节点与当前根节点不同值,则左子树取第二小,与右子树根节点比。
// 注意,左子树如果没有第二小,会返回负数
return
getMinOne
(
findSecondMinimumValue
(
root
.
left
),
val_right
);
}
else
if
(
val_root
!=
val_left
&&
val_root
==
val_right
){
// 类似上面的
return
getMinOne
(
val_left
,
findSecondMinimumValue
(
root
.
right
));
}
else
{
return
getMinOne
(
findSecondMinimumValue
(
root
.
left
),
findSecondMinimumValue
(
root
.
right
));
}
}
int
getMinOne
(
int
x
,
int
y
){
if
(
x
==-
1
&&
y
==-
1
){
return
-
1
;
}
else
if
(
x
==-
1
&&
y
!=-
1
){
return
y
;
}
else
if
(
x
!=-
1
&&
y
==-
1
){
return
x
;
}
else
{
return
Math
.
min
(
x
,
y
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL