-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution530.java
More file actions
41 lines (34 loc) · 791 Bytes
/
Copy pathSolution530.java
File metadata and controls
41 lines (34 loc) · 791 Bytes
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
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 二叉搜索树的最小绝对差
* @date: 2019/03/02
*/
public class Solution530 {
TreeNode pre;
int mn = Integer.MAX_VALUE;
public int getMinimumDifference(TreeNode root) {
inOrder(root);
return mn;
}
public void inOrder(TreeNode node) {
if (null == node) {
return;
}
inOrder(node.left);
if (null != pre) {
mn = Math.min(mn, node.val - pre.val);
}
// 遍历右子树时,pre引用需要更新为node
pre = node;
inOrder(node.right);
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}