From a88f761eb08b5c4d8a3484633e9dc77092652e86 Mon Sep 17 00:00:00 2001 From: leehyeyun Date: Wed, 3 Dec 2025 16:08:28 +0900 Subject: [PATCH 1/2] =?UTF-8?q?4=EC=A3=BC=EC=B0=A8=20=EB=AC=B8=EC=A0=9C=20?= =?UTF-8?q?=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- merge-two-sorted-lists/leehyeyun.js | 91 +++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 merge-two-sorted-lists/leehyeyun.js diff --git a/merge-two-sorted-lists/leehyeyun.js b/merge-two-sorted-lists/leehyeyun.js new file mode 100644 index 0000000000..68e458ec65 --- /dev/null +++ b/merge-two-sorted-lists/leehyeyun.js @@ -0,0 +1,91 @@ +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ + +/* + 두 개의 정렬된 연결 리스트 list1과 list2의 head가 주어진다. + + 두 리스트의 노드를 이어 붙여 하나의 정렬된 리스트를 만들고, + 그 병합된 연결 리스트의 head를 반환하는 함수. + + Example 1: + Input: list1 = [1,2,4], list2 = [1,3,4] + Output: [1,1,2,3,4,4] + + Example 2: + Input: list1 = [] + list2 = [] + Output: [] + + Example 3: + Input: list1 = [] + list2 = [0] + Output: [0] + + Constraints: + - 두 리스트의 노드 개수: 0 ~ 50 + - 노드 값 범위: -100 ~ 100 + - list1과 list2는 모두 오름차순(Non-decreasing order)으로 정렬됨 +*/ +/** + * @param {ListNode} list1 + * @param {ListNode} list2 + * @return {ListNode} + */ + +function ListNode(val, next) { + this.val = (val===undefined ? 0 : val) + this.next = (next===undefined ? null : next) +} + +var mergeTwoLists = function(list1, list2) { + //새로운 노드 생성 -> -1이라는 값은 쓰레기값 + let dummy = new ListNode(-1); + let current = dummy; + + while (list1 !== null && list2 !== null) { + //더 작은 값을 가진 노드를 현재 노드 뒤에 붙여줌 + if (list1.val < list2.val) { + current.next = list1; + list1 = list1.next; //다음 노드로 이동 + } else { + current.next = list2; + list2 = list2.next; //다음 노드로 이동 + } + current = current.next; //새 노드를 붙인 뒤 이동 + } + + // 둘 중 하나가 남아 있으면 이어붙이기 + if (list1 !== null) current.next = list1; + if (list2 !== null) current.next = list2; + + //dummy -1값은 제외하고 return하기 위해 dummy.next를 반환 + return dummy.next; +}; + +// Example 1 +const list1 = new ListNode(1, new ListNode(2, new ListNode(4))); +const list2 = new ListNode(1, new ListNode(3, new ListNode(4))); + +console.log("Example 1:"); +console.log(JSON.stringify(mergeTwoLists(list1, list2), null, 2)); + +// Example 2 +const list1_ex2 = null; +const list2_ex2 = null; + +console.log("Example 2:"); +console.log(JSON.stringify(mergeTwoLists(list1_ex2, list2_ex2), null, 2)); + +// Example 3 +const list1_ex3 = null; +const list2_ex3 = new ListNode(0); + +console.log("Example 3:"); +console.log(JSON.stringify(mergeTwoLists(list1_ex3, list2_ex3), null, 2)); + + From d155be4b7f6d6650afd705928371098e1f9bc5ae Mon Sep 17 00:00:00 2001 From: leehyeyun Date: Fri, 5 Dec 2025 15:29:17 +0900 Subject: [PATCH 2/2] =?UTF-8?q?4=EC=A3=BC=EC=B0=A8=20=EB=AC=B8=EC=A0=9C=20?= =?UTF-8?q?=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maximum-depth-of-binary-tree/leehyeyun.js | 70 +++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 maximum-depth-of-binary-tree/leehyeyun.js diff --git a/maximum-depth-of-binary-tree/leehyeyun.js b/maximum-depth-of-binary-tree/leehyeyun.js new file mode 100644 index 0000000000..0612e84440 --- /dev/null +++ b/maximum-depth-of-binary-tree/leehyeyun.js @@ -0,0 +1,70 @@ +/** + * Definition for a binary tree node. + */ +function TreeNode(val, left, right) { + this.val = (val === undefined ? 0 : val); + this.left = (left === undefined ? null : left); + this.right = (right === undefined ? null : right); +} +/* + 이진 트리(Binary Tree)의 root 노드가 주어졌을 때, + 트리의 최대 깊이(maximum depth)를 반환하는 함수. + + 최대 깊이란: + - 루트(root)에서 가장 먼 리프(leaf) 노드까지 + 도달하는 경로에 포함된 "노드의 개수"를 의미함. + + Example 1: + Input: root = [3,9,20,null,null,15,7] + Output: 3 + 설명: + 트리 구조는 다음과 같으며, + 가장 깊은 경로(3 → 20 → 15 또는 3 → 20 → 7)의 노드 수는 3. + + Example 2: + Input: root = [1,null,2] + Output: 2 + 설명: + 트리 구조는 1 → 2 형태이며 노드 수 2가 최대 깊이. + + Constraints: + - 트리의 노드 개수: 0 ~ 10^4 + - 노드 값 범위: -100 ~ 100 +*/ + +/** + * @param {TreeNode} root + * @return {number} + */ +var maxDepth = function(root) { + + if (root === null) return 0; + + const left = maxDepth(root.left); + const right = maxDepth(root.right); + + return 1 + Math.max(left, right); +}; + +// example1 +const example1 = new TreeNode( + 3, + new TreeNode(9), + new TreeNode(20, + new TreeNode(15), + new TreeNode(7) + ) +); + +// example2 +const example2 = new TreeNode( + 1, + null, + new TreeNode(2) +); + + +// maxDepth 함수 결과 출력 +console.log("Example 1 maxDepth:", maxDepth(example1)); +console.log("Example 2 maxDepth:", maxDepth(example2)); +