FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_02/id_13/LeetCode_783_13.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_13
/
LeetCode_783_13.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
51 lines (40 loc) · 1.15 KB
Breadcrumbs
algorithm
/
Week_02
/
id_13
/
LeetCode_783_13.java
Copy path
File metadata and controls
51 lines (40 loc) · 1.15 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
package
leetCode
.
week2
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
public
class
LeetCode_783_13
{
private
List
<
Integer
>
list
=
new
ArrayList
<>();
/**
* 因为二叉搜索树的特性:
* 如果左右子树均为有效的话,左子树小于父节点,右子树大于父节点
* <p>
* 通过中序遍历,按照从小到大的顺序访问整个树
* 相邻节点的差值取小的
*
* 时间复杂度是O(N + M)
*
* @param root 根节点
* @return 最小值
*/
public
int
minDiffInBST
(
TreeNode
root
) {
int
cur
=
Integer
.
MAX_VALUE
;
inOrder
(
root
);
for
(
int
i
=
1
;
i
<
list
.
size
();
i
++) {
cur
=
Math
.
min
(
list
.
get
(
i
) -
list
.
get
(
i
-
1
),
cur
);
}
return
cur
;
}
private
void
inOrder
(
TreeNode
node
) {
if
(
node
==
null
)
return
;
inOrder
(
node
.
left
);
list
.
add
(
node
.
val
);
inOrder
(
node
.
right
);
}
public
class
TreeNode
{
int
val
;
TreeNode
left
;
TreeNode
right
;
TreeNode
(
int
x
) {
val
=
x
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL