leetcode530_二叉搜索树的最小绝对差
显然二叉搜索树中两两间距最小的点一定是对该树进行中序遍历得到的:
class Solution {
private int ans = Integer.MAX_VALUE;
private TreeNode pre = null;
public int getMinimumDifference(TreeNode root) {
traversal(root);
return ans;
}
private void traversal(TreeNode root) {
if(root == null) return;
traversal(root.left);
if(pre != null) {
int dis = root.val - pre.val;
ans = dis < ans ? dis : ans;
}
pre = root;
traversal(root.right);
}
}