We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent a5c3fed commit acf82dfCopy full SHA for acf82df
solution/0783.Minimum Distance Between BST Nodes/Solution.java
@@ -0,0 +1,20 @@
1
+class Solution {
2
+ public int minDiffInBST(TreeNode root) {
3
+ TreeNode[] pre = new TreeNode[1];
4
+ int[] res = new int[]{Integer.MAX_VALUE};
5
+ dfs(root, pre, res);
6
+ return res[0];
7
+ }
8
+
9
+ private void dfs(TreeNode root, TreeNode[] pre, int[] res) {
10
+ if (root == null) {
11
+ return;
12
13
+ dfs(root.left, pre, res);
14
+ if (pre[0] != null) {
15
+ res[0] = Math.min(res[0], root.val - pre[0].val);
16
17
+ pre[0] = root;
18
+ dfs(root.right, pre, res);
19
20
+}
0 commit comments