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
- dynamic programming
- 자바
- Binary Tree
- Data Structure
- SQL
- 코딩테스트
- geometry
- Math
- bit manipulation
- 파이썬
- simulation
- string
- sorting
- Counting
- Tree
- 구현
- 코테
- Binary Search
- two pointers
- Method
- hash table
- Stack
- Matrix
- database
- Class
- greedy
- implement
- Number Theory
- array
- java
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 783. Minimum Distance Between BST Nodes 본문
1. Input
1) TreeNode root
2. Output
1) root에서 고른 임의의 두 값의 차가 가장 작은 것을 반환
- 차는 0 이상이어야 한다.
3. Constraint
1) 노드 수의 범위는 [2, 100]이다.
2) 0 <= Node.val <= 10^5
4. Example
Input: root = [4,2,6,1,3] -> Output: 1
Input: root = [1,0,48,null,null,12,49] -> Output: 1
5. Code
1) 첫 코드(2023/05/31)
/**
* 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 minDiffInBST(TreeNode root) {
int min = Integer.MAX_VALUE;
List<Integer> list = inorder(root);
for(int i=1 ; i<list.size() ; i++)
min = Math.min(min, list.get(i)-list.get(i-1));
return min;
}
static List<Integer> inorder(TreeNode root) {
List<Integer> ans = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode c = root;
while(!stack.isEmpty() || c!=null) {
if(c != null) {
stack.push(c);
c = c.left;
} else {
TreeNode node = stack.pop();
ans.add(node.val);
c = node.right;
}
}
return ans;
}
}
- 19%, 5%
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 876. Middle of the Linked List (0) | 2023.05.31 |
---|---|
[LeetCode/Easy] 830. Positions of Large Groups (0) | 2023.05.31 |
[프로그래머스/Lv.0] 저주의 숫자 3 (0) | 2023.05.30 |
[프로그래머스/Lv.0] 등수 매기기 (0) | 2023.05.30 |
[LeetCode/Easy] 706. Design HashMap (0) | 2023.05.30 |