# LeetCode-Alg-530-Minimum Absolute Difference in BST

## LeetCode-Alg-530-Minimum Absolute Difference in BST

Posted by Jae on May 8, 2019

### 1、题目

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.

Example:

Input:

1
\
3
/
2

Output:
1

Explanation:
The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).


Note: There are at least two nodes in this BST.

### 4、实现

public  class Solution {
private int min = Integer.MAX_VALUE;

public int getMinimumDifference(TreeNode root) {
if (root == null || (root.left == null && root.right == null)){
return -1;
}

int minDiff = Integer.MAX_VALUE;

min = Math.min(getMinDiff(root), min);
getMinimumDifference(root.left);
getMinimumDifference(root.right);

return min;
}

public int getMinDiff(TreeNode root){
if (root == null){
return Integer.MAX_VALUE;
}
int leftDiff = Integer.MAX_VALUE;
int rightDiff = Integer.MAX_VALUE;
int minDiff = Integer.MAX_VALUE;

if (root.left != null){
leftDiff = root.val - getNode(root.left).val;
}
if (root.right != null) {
rightDiff = getNode2(root.right).val - root.val;
}

minDiff = Math.min(minDiff, leftDiff);
minDiff = Math.min(minDiff, rightDiff);

return minDiff;
}

// 给一个根节点，去左子树找最接近根的节点
private TreeNode getNode(TreeNode root){
if (root.right == null){
return root;
}
return getNode(root.right);
}

private TreeNode getNode2(TreeNode root){
if (root.left == null){
return root;
}
return getNode2(root.left);
}
}


### 5、优化

public  class Solution {
private int min = Integer.MAX_VALUE;
// 保存中序遍历中上一个节点的值
private Integer preValue;

public int getMinimumDifference(TreeNode root){
inOrder(root);
return min;
}

private void inOrder(TreeNode root){
if (root == null){
return;
}
inOrder(root.left);
if (preValue != null){
min = Math.min(min, root.val - preValue);
}
preValue = root.val;
inOrder(root.right);
}
}


< script src = " / js / bootstrap.min.js " >