Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4690553

Browse files
authored
Create 530. Minimum Absolute Difference in BST
1 parent 8d6d3ac commit 4690553

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 minDifference = Integer.MAX_VALUE;
18+
Integer prev = null;
19+
public int getMinimumDifference(TreeNode root) {
20+
21+
//Exit Condition
22+
if(root == null) return minDifference;
23+
24+
getMinimumDifference(root.left);
25+
26+
//Process Root
27+
if(prev != null){
28+
minDifference = Math.min(minDifference, root.val-prev);
29+
}
30+
31+
if(root != null){
32+
prev = root.val;
33+
}
34+
35+
getMinimumDifference(root.right);
36+
37+
return minDifference;
38+
}
39+
}
40+

0 commit comments

Comments
 (0)