783. 二叉搜寻树节点最小间隔
给你一个二叉搜寻树的根节点 root,返回 树中任意两不同节点值之间的最小差值。留神:本题与 530:https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/ 雷同
示例 1:输出:root = [4,2,6,1,3]
输入:1
示例 2:输出:root = [1,0,48,null,null,12,49]
输入:1
class Solution {
//DFS
TreeNode pre;
int min = Integer.MAX_VALUE;
public int minDiffInBST(TreeNode root) {dfs(root);
return min;
}
public void dfs(TreeNode root){if(root == null) return;
dfs(root.left);
if(pre != null) min = Math.min(min,root.val - pre.val);
pre = root;
dfs(root.right);
}
}