Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- java
- Counting
- dynamic programming
- Math
- implement
- hash table
- Binary Search
- SQL
- 코딩테스트
- Method
- simulation
- array
- Tree
- bit manipulation
- Matrix
- string
- sorting
- Class
- 코테
- 구현
- geometry
- Binary Tree
- Stack
- 자바
- Data Structure
- database
- Number Theory
- 파이썬
- two pointers
- greedy
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 530. Minimum Absolute Difference in BST 본문
1. Input
1) TreeNode root
2. Output
1) root에 있는 두 노드의 값의 차가 가장 작은 것을 반환
- 차는 절댓값으로 계산한다.
3. Constraint
1) 노드의 수는 [2, 10^4]이다.
2) 0 <= Node.val <= 10^5
4. Example
Input: root = [4,2,6,1,3] -> Output: 1
5. Code
1) 첫 코드(2023/05/23)
/**
* 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 {
public int getMinimumDifference(TreeNode root) {
int min = Integer.MAX_VALUE;
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.empty()){
TreeNode cur = stack.pop();
list.add(cur.val);
if(cur.left!=null)
stack.push(cur.left);
if(cur.right!=null)
stack.push(cur.right);
}
list.sort(Comparator.naturalOrder());
for(int i=0 ; i<list.size()-1 ; i++)
min = Math.min(min, list.get(i+1)-list.get(i));
return min;
}
}
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[백준 온라인 저지] 1431. 시리얼 번호 (0) | 2023.05.24 |
---|---|
[백준 온라인 저지] 1181. 단어 정렬 (0) | 2023.05.24 |
[LeetCode/Easy] 506. Relative Ranks (0) | 2023.05.23 |
[백준 온라인 저지] 2752. 세수정렬 (0) | 2023.05.20 |
[LeetCode/Easy] 485. Max Consecutive Ones (0) | 2023.05.19 |