Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions kth-smallest-element-in-a-bst/njngwn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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 cnt = 0;
int result = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다음에 제안한 조기 종료를 위해 Sentinel Value로 변경합니다.

Suggested change
int result = 0;
int result = -1;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 그렇군요!! 하나 배워갑니다! 피드백 감사드려요☺️☺️


public int kthSmallest(TreeNode root, int k) {
cnt = k;
traverseInOrder(root);
return result;
}

public void traverseInOrder (TreeNode node) {
if (node == null) return;

if (node.left != null) traverseInOrder(node.left);
cnt--;
if (cnt == 0) {
result = node.val;
return;
}
if (node.right != null) traverseInOrder(node.right);
Copy link
Contributor

@yhkee0404 yhkee0404 Oct 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 하면 가지치기 가능할 것 같아요!

Suggested change
if (node.right != null) traverseInOrder(node.right);
if (result == -1 && node.right != null) traverseInOrder(node.right);

}
}
23 changes: 23 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/njngwn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/

class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root.val == p.val || root.val == q.val) return root;

if (p.val < root.val && q.val < root.val) {
return lowestCommonAncestor(root.left, p, q);
} else if (p.val > root.val && q.val > root.val) {
return lowestCommonAncestor(root.right, p, q);
} else {
return root;
}
}
}
30 changes: 30 additions & 0 deletions meeting-rooms/njngwn.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정렬 후 바로 전,후 시간만 간단하게 비교하는 부분이 너무 잘 구현된것 같아요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Definition of Interval:
* public class Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/

public class Solution {
/**
* @param intervals: an array of meeting time intervals
* @return: if a person could attend all meetings
*/
public boolean canAttendMeetings(List<Interval> intervals) {
// Write your code here
intervals.sort((a, b) -> a.start - b.start); // sort by start time

int prevEndTime = 0;
for (Interval meeting : intervals) {
if (meeting.start < prevEndTime) {
return false;
}
prevEndTime = meeting.end;
}
return true;
}
}