Skip to content

[moonhyeok] Week 12 #1051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 1, 2025
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
31 changes: 31 additions & 0 deletions remove-nth-node-from-end-of-list/mike2ox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Source: https://leetcode.com/problems/remove-nth-node-from-end-of-list/
* Solution: 두 개의 포인터를 이용해 n번째 노드를 찾아 삭제
*
* 시간복잡도: O(N) - 두 개의 포인터가 한번씩 순회
* 공간복잡도: O(1) - 상수만큼의 공간 사용
*/

function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
if (!head) return null;

const dummy = new ListNode(0, head);
let slow: ListNode | null = dummy;
let fast: ListNode | null = dummy;

for (let i = 0; i <= n; i++) {
if (!fast) return head;
fast = fast.next;
}

while (fast) {
slow = slow!.next;
fast = fast.next;
}

if (slow && slow.next) {
slow.next = slow.next.next;
}

return dummy.next;
}
60 changes: 60 additions & 0 deletions same-tree/mike2ox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Source: https://leetcode.com/problems/same-tree/
* Solution: 트리의 노드를 순회하면서 값이 같은지 확인
* 시간 복잡도: O(N) - 트리의 모든 노드를 한번씩 방문
* 공간 복잡도: O(N) - 스택에 최대 트리의 높이만큼 쌓일 수 있음
*
* 추가 사항
* - 트리를 순회만 하면 되기에 Typescript로 Stack을 활용해 DFS로 해결
* - 재귀로 구현하면 간단하게 구현 가능
*/

/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
if (!q || !p) return !q === !p;
let result = true;
let stack = new Array({
left: p,
right: q,
});
while (stack.length) {
const now = stack.pop();
const left = now?.left;
const right = now?.right;
const isLeafNode =
!left?.left && !left?.right && !right?.right && !right?.left;
const isSameValue = left?.val === right?.val;
const hasDifferentSubtree =
(!left?.left && right?.left) || (!left?.right && right?.right);
if (isLeafNode && isSameValue) continue;
if (!isSameValue || hasDifferentSubtree) {
result = false;
break;
}
stack.push({ left: left?.left, right: right?.left });
stack.push({ left: left?.right, right: right?.right });
}
return result;
}

// Solution 2 - 재귀
function isSameTree2(p: TreeNode | null, q: TreeNode | null): boolean {
if (!p && !q) return true;
if (!p || !q) return false;
if (p.val !== q.val) return false;

return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}