forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution2.java
34 lines (26 loc) · 847 Bytes
/
Solution2.java
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
/// Source : https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/
/// Author : liuyubobobo
/// Time : 2018-04-30
import java.util.ArrayList;
import java.util.Collections;
/// Using inorder to traverse all nodes
///
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution2 {
public int getMinimumDifference(TreeNode root) {
ArrayList<Integer> list = new ArrayList<>();
inOrder(root, list);
int res = Integer.MAX_VALUE;
for(int i = 1 ; i < list.size() ; i ++)
res = Math.min(res, list.get(i) - list.get(i - 1));
return res;
}
private void inOrder(TreeNode node, ArrayList<Integer> list){
if(node == null)
return;
inOrder(node.left, list);
list.add(node.val);
inOrder(node.right, list);
}
}