-
-
Notifications
You must be signed in to change notification settings - Fork 305
[leehyeyun] WEEK 04 solutions #2144
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,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)); | ||
|
|
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,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)); | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 처음에 왜 붙어야 하는지 몰랐으나 조금 생각해보면 while문이 끝나는 조건은 둘중 하나가 Null이 될때이므로 나머지는 남아 있다는 의미이므로 남은 부분에 대해 붙여줘야하는 로직이 들어가는 것을 알게 되었습니다.