Given a non-negative all nodes of the binary search tree, the absolute value of the minimum difference between two arbitrary nodes in the tree request.
Example:
Input:
1
\
3
/
2
Output:
1
Explanation: The
minimum absolute difference of 1, wherein the absolute value of difference of 1 and 2 is 1 (or 2 and 3).
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int num = Integer.MAX_VALUE;
TreeNode pre = null;
public int getMinimumDifference(TreeNode root) {
inOrder(root);
return num;
}
public void inOrder(TreeNode root){
if(root == null){
return ;
}
inOrder(root.left);
if(pre!=null){
num = Math.min(num, root.val - pre.val);
}
pre = root;
inOrder(root.right);
}
}