-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ530MinimumAbsoluteDifference.java
More file actions
44 lines (39 loc) · 1.18 KB
/
Q530MinimumAbsoluteDifference.java
File metadata and controls
44 lines (39 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
@b-knd (jingru) on 10 August 20222 10:32:00
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int min = Integer.MAX_VALUE;
TreeNode prev;
public int getMinimumDifference(TreeNode root) {
getMin(root);
return min;
}
/*
Concept:
- use inorder traversal as inorder travel node in sorted increasing manner and we can find minimum by comparing current value and previous value
*/
public void getMin(TreeNode node){
if(node == null) return;
getMin(node.left);
if(prev != null) min = Math.min(Math.abs(node.val-prev.val), min);
prev = node;
getMin(node.right);
}
}
//Runtime: 0 ms, faster than 100.00% of Java online submissions for Minimum Absolute Difference in BST.
//Memory Usage: 42.2 MB, less than 91.41% of Java online submissions for Minimum Absolute Difference in BST.