From b0dbcb23d6f536bdee4d044dd88c91d76212612b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sat, 26 Oct 2024 22:30:04 +0900 Subject: [PATCH 1/3] Add week 11 solutions: maximum-depth-of-binary-tree --- maximum-depth-of-binary-tree/gitsunmin.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 maximum-depth-of-binary-tree/gitsunmin.ts diff --git a/maximum-depth-of-binary-tree/gitsunmin.ts b/maximum-depth-of-binary-tree/gitsunmin.ts new file mode 100644 index 000000000..22770684d --- /dev/null +++ b/maximum-depth-of-binary-tree/gitsunmin.ts @@ -0,0 +1,22 @@ +/** + * https://leetcode.com/problems/maximum-depth-of-binary-tree/ + * time complexity : O(n) + * space complexity : O(n) + */ + +export 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) + } +} + +export function maxDepth(root: TreeNode | null): number { + if (!root) return 0; + + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; +}; \ No newline at end of file From 353b541bf7e06c5e8e206c1a3792d6d5e1b1a51e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sun, 27 Oct 2024 00:20:42 +0900 Subject: [PATCH 2/3] Add week 11 solutions: reorder-list --- reorder-list/gitsunmin.ts | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 reorder-list/gitsunmin.ts diff --git a/reorder-list/gitsunmin.ts b/reorder-list/gitsunmin.ts new file mode 100644 index 000000000..4718d3234 --- /dev/null +++ b/reorder-list/gitsunmin.ts @@ -0,0 +1,53 @@ +/** + * https://leetcode.com/problems/reorder-list/ + * time complexity : O(n) + * space complexity : O(1) + */ + +class ListNode { + val: number + next: ListNode | null + constructor(val?: number, next?: ListNode | null) { + this.val = (val === undefined ? 0 : val) + this.next = (next === undefined ? null : next) + } +} + +function reorderList(head: ListNode | null): void { + if (!head || !head.next) return; + + // 1. 중간 지점 찾기 (Floyd’s Tortoise and Hare) + let slow: ListNode | null = head; + let fast: ListNode | null = head; + + while (fast && fast.next) { + slow = slow!.next; + fast = fast.next.next; + } + + // 2. 중간 이후의 리스트 뒤집기 + let prev: ListNode | null = null; + let curr: ListNode | null = slow; + + while (curr) { + const next = curr.next; + curr.next = prev; + prev = curr; + curr = next; + } + + // 3. 앞부분과 뒤집어진 후반부 병합 + let first: ListNode | null = head; + let second: ListNode | null = prev; + + while (second && second.next) { + const temp1 = first!.next; + const temp2 = second.next; + + first!.next = second; + second.next = temp1; + + first = temp1; + second = temp2; + } +}; \ No newline at end of file From c6d6330076801a166b27dfd4359f535401b5e63c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Sun, 27 Oct 2024 00:24:14 +0900 Subject: [PATCH 3/3] add line break --- maximum-depth-of-binary-tree/gitsunmin.ts | 2 +- reorder-list/gitsunmin.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/maximum-depth-of-binary-tree/gitsunmin.ts b/maximum-depth-of-binary-tree/gitsunmin.ts index 22770684d..befcce845 100644 --- a/maximum-depth-of-binary-tree/gitsunmin.ts +++ b/maximum-depth-of-binary-tree/gitsunmin.ts @@ -19,4 +19,4 @@ export function maxDepth(root: TreeNode | null): number { if (!root) return 0; return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; -}; \ No newline at end of file +}; diff --git a/reorder-list/gitsunmin.ts b/reorder-list/gitsunmin.ts index 4718d3234..fa15c0659 100644 --- a/reorder-list/gitsunmin.ts +++ b/reorder-list/gitsunmin.ts @@ -50,4 +50,4 @@ function reorderList(head: ListNode | null): void { first = temp1; second = temp2; } -}; \ No newline at end of file +};