Skip to content

Commit acf82df

Browse files
authored
Create Solution.java
1 parent a5c3fed commit acf82df

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)