力扣 LeetCode 530. 二叉搜索树的最小绝对差(Day10:二叉树)
解题思路:
中序遍历,与前一个节点的值进行比较
class Solution {
Integer min = Integer.MAX_VALUE;
TreeNode pre = null;
public int getMinimumDifference(TreeNode root) {
traversal(root);
return min;
}
public void traversal(TreeNode cur) {
if (cur == null) return;
traversal(cur.left);
if (pre != null) {
if (min > cur.val - pre.val)
min = cur.val - pre.val;
}
pre = cur;
traversal(cur.right);
}
}