给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/*一看题解索然无味
*利用中序遍历有序的特点,递归查找最小相邻节点差值
*/
class Solution {
int min = Integer.MAX_VALUE;
TreeNode preNode;
public int getMinimumDifference(TreeNode root) {
inorder(root);
return min;
}
public void inorder(TreeNode root){
if(root==null)
return ;
inorder(root.left);
if(preNode!=null)
min = Math.min(min,root.val-preNode.val);
preNode = root;
inorder(root.right);
}
}