Skip to content

Commit e826ba4

Browse files
authored
Merge pull request #2144 from leehyeyun/main
[leehyeyun] WEEK 04 solutions
2 parents 4102a3e + d155be4 commit e826ba4

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Definition for a binary tree node.
3+
*/
4+
function TreeNode(val, left, right) {
5+
this.val = (val === undefined ? 0 : val);
6+
this.left = (left === undefined ? null : left);
7+
this.right = (right === undefined ? null : right);
8+
}
9+
/*
10+
이진 트리(Binary Tree)의 root 노드가 주어졌을 때,
11+
트리의 최대 깊이(maximum depth)를 반환하는 함수.
12+
13+
최대 깊이란:
14+
- 루트(root)에서 가장 먼 리프(leaf) 노드까지
15+
도달하는 경로에 포함된 "노드의 개수"를 의미함.
16+
17+
Example 1:
18+
Input: root = [3,9,20,null,null,15,7]
19+
Output: 3
20+
설명:
21+
트리 구조는 다음과 같으며,
22+
가장 깊은 경로(3 → 20 → 15 또는 3 → 20 → 7)의 노드 수는 3.
23+
24+
Example 2:
25+
Input: root = [1,null,2]
26+
Output: 2
27+
설명:
28+
트리 구조는 1 → 2 형태이며 노드 수 2가 최대 깊이.
29+
30+
Constraints:
31+
- 트리의 노드 개수: 0 ~ 10^4
32+
- 노드 값 범위: -100 ~ 100
33+
*/
34+
35+
/**
36+
* @param {TreeNode} root
37+
* @return {number}
38+
*/
39+
var maxDepth = function(root) {
40+
41+
if (root === null) return 0;
42+
43+
const left = maxDepth(root.left);
44+
const right = maxDepth(root.right);
45+
46+
return 1 + Math.max(left, right);
47+
};
48+
49+
// example1
50+
const example1 = new TreeNode(
51+
3,
52+
new TreeNode(9),
53+
new TreeNode(20,
54+
new TreeNode(15),
55+
new TreeNode(7)
56+
)
57+
);
58+
59+
// example2
60+
const example2 = new TreeNode(
61+
1,
62+
null,
63+
new TreeNode(2)
64+
);
65+
66+
67+
// maxDepth 함수 결과 출력
68+
console.log("Example 1 maxDepth:", maxDepth(example1));
69+
console.log("Example 2 maxDepth:", maxDepth(example2));
70+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
9+
/*
10+
두 개의 정렬된 연결 리스트 list1과 list2의 head가 주어진다.
11+
12+
두 리스트의 노드를 이어 붙여 하나의 정렬된 리스트를 만들고,
13+
그 병합된 연결 리스트의 head를 반환하는 함수.
14+
15+
Example 1:
16+
Input: list1 = [1,2,4], list2 = [1,3,4]
17+
Output: [1,1,2,3,4,4]
18+
19+
Example 2:
20+
Input: list1 = []
21+
list2 = []
22+
Output: []
23+
24+
Example 3:
25+
Input: list1 = []
26+
list2 = [0]
27+
Output: [0]
28+
29+
Constraints:
30+
- 두 리스트의 노드 개수: 0 ~ 50
31+
- 노드 값 범위: -100 ~ 100
32+
- list1과 list2는 모두 오름차순(Non-decreasing order)으로 정렬됨
33+
*/
34+
/**
35+
* @param {ListNode} list1
36+
* @param {ListNode} list2
37+
* @return {ListNode}
38+
*/
39+
40+
function ListNode(val, next) {
41+
this.val = (val===undefined ? 0 : val)
42+
this.next = (next===undefined ? null : next)
43+
}
44+
45+
var mergeTwoLists = function(list1, list2) {
46+
//새로운 노드 생성 -> -1이라는 값은 쓰레기값
47+
let dummy = new ListNode(-1);
48+
let current = dummy;
49+
50+
while (list1 !== null && list2 !== null) {
51+
//더 작은 값을 가진 노드를 현재 노드 뒤에 붙여줌
52+
if (list1.val < list2.val) {
53+
current.next = list1;
54+
list1 = list1.next; //다음 노드로 이동
55+
} else {
56+
current.next = list2;
57+
list2 = list2.next; //다음 노드로 이동
58+
}
59+
current = current.next; //새 노드를 붙인 뒤 이동
60+
}
61+
62+
// 둘 중 하나가 남아 있으면 이어붙이기
63+
if (list1 !== null) current.next = list1;
64+
if (list2 !== null) current.next = list2;
65+
66+
//dummy -1값은 제외하고 return하기 위해 dummy.next를 반환
67+
return dummy.next;
68+
};
69+
70+
// Example 1
71+
const list1 = new ListNode(1, new ListNode(2, new ListNode(4)));
72+
const list2 = new ListNode(1, new ListNode(3, new ListNode(4)));
73+
74+
console.log("Example 1:");
75+
console.log(JSON.stringify(mergeTwoLists(list1, list2), null, 2));
76+
77+
// Example 2
78+
const list1_ex2 = null;
79+
const list2_ex2 = null;
80+
81+
console.log("Example 2:");
82+
console.log(JSON.stringify(mergeTwoLists(list1_ex2, list2_ex2), null, 2));
83+
84+
// Example 3
85+
const list1_ex3 = null;
86+
const list2_ex3 = new ListNode(0);
87+
88+
console.log("Example 3:");
89+
console.log(JSON.stringify(mergeTwoLists(list1_ex3, list2_ex3), null, 2));
90+
91+

0 commit comments

Comments
 (0)