Skip to content

Commit 2207441

Browse files
authored
Merge pull request #2126 from hozzijeong/main
[hozzijeong] WEEK 04 solutions
2 parents c177744 + 3bfd185 commit 2207441

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var findMin = function(nums) {
6+
return Math.min(...nums)
7+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
var maxDepth = function(root) {
14+
if(!root) return 0
15+
16+
const dfs = (node, level) =>{
17+
if(!node) return level;
18+
19+
let left = level;
20+
let right = level
21+
22+
if(node.left){
23+
left = dfs(node.left, level+1)
24+
}
25+
26+
if(node.right){
27+
right = dfs(node.right, level+1)
28+
}
29+
30+
return Math.max(left,right);
31+
}
32+
33+
return dfs(root,1)
34+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
* @param {ListNode} list1
10+
* @param {ListNode} list2
11+
* @return {ListNode}
12+
*/
13+
14+
/**
15+
* list๋Š” ์˜ค๋ฆ„ ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋‘ ๊ฐœ์˜ ๋ฆฌ์ŠคํŠธ์˜ head๊ฐ€ ๋” ์ž‘์€ ๊ฐ’์ด listNode์˜ next์— ์ˆœ์„œ๋Œ€๋กœ ๋“ค์–ด์˜ค๊ฒŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
16+
* list ๋‘˜ ์ค‘ํ•˜๋‚˜๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ๋‚˜๋จธ์ง€ ๋‹ค๋ฅธ ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
17+
* ๋‘ ํ—ค๋“œ ์ค‘์—์„œ ๋” ์ž‘์€ ๊ฐ’์˜ next์— ์žฌ๊ท€์ ์œผ๋กœ ๋‹ค์Œ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋„˜๊ฒจ์ฃผ๋ฉด์„œ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค
18+
*
19+
*/
20+
21+
var mergeTwoLists = function(list1, list2) {
22+
if(!list1 || !list2) return list1 || list2
23+
24+
if(list1.val < list2.val){
25+
list1.next = mergeTwoLists(list1.next,list2)
26+
return list1
27+
}
28+
29+
list2.next = mergeTwoLists(list2.next,list1);
30+
31+
return list2;
32+
};

0 commit comments

Comments
ย (0)