-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
[moonhyeok] Week 12 #1051
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.