Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e87ca85

Browse files
committed
Time: 0 ms (100.00%), Space: 40.2 MB (35.81%) - LeetHub
1 parent 71c0e2e commit e87ca85

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
int minDiff = Integer.MAX_VALUE;
18+
TreeNode prevValue;
19+
20+
public int minDiffInBST(TreeNode root) {
21+
inOrder(root);
22+
return minDiff;
23+
24+
}
25+
26+
private void inOrder(TreeNode root) {
27+
if (root == null) {
28+
return;
29+
}
30+
31+
inOrder(root.left);
32+
33+
if (prevValue != null) {
34+
minDiff = Math.min(minDiff, root.val - prevValue.val);
35+
}
36+
37+
prevValue = root;
38+
39+
inOrder(root.right);
40+
}
41+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /