-
-
Notifications
You must be signed in to change notification settings - Fork 304
[casentino] WEEK 04 Solutions #2133
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
Conversation
ys-han00
left a comment
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.
날씨가 많이 추워진 한 주네요!
아직 모든 문제를 풀이 중이시지만, 먼저 풀어두신 문제들에 대해서 리뷰를 남겨두었습니다. 😊
제가 리뷰에 아직 익숙하지 않아 부족한 부분이 있을 수 있으니,
이상한 점이나 궁금한 점이 있으시면 언제든지 PR 멘션이나 Discord로 편하게 말씀해주세요!
남은 문제들도 화이팅하시고, 추운 날씨에 감기 조심하세요! ❄️💪
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.
보기 좋은 이분 탐색 코드네요 👍
| 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; | ||
| } | ||
| } |
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.
주석을 풀어 두신 이유가 있을까요??
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.
타입스크립트 컴파일 에러 나길래 선언만 해두었어요!
| function maxDepth(root: TreeNode | null): number { | ||
| function recursive(node: TreeNode | null, count: number) { | ||
| if (!node) { | ||
| return count; | ||
| } | ||
| const left = recursive(node.left, count + 1); | ||
| const right = recursive(node.right, count + 1); | ||
| return left > right ? left : right; | ||
| } | ||
| return recursive(root, 0); | ||
| } |
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.
알고리즘을 아래처럼 이해했는데, 맞게 이해했다면 좋은 로직이네요!
Leaf Node의 자식은NULL값으로 더 갈 수 없으니 깊이count를 반환한다.Node의 왼쪽(left), 오른쪽(right) 나누어 DFS를 진행하고 그 중 더 큰 값을 반환한다.
현재 함수는 count를 매개변수로 전달하면서 깊이를 추적하고 있는데요,
깊이를 인자로 들고 다니지 않고, 재귀의 반환 값 자체가 깊이가 되도록 만드는 방식도 한 번 고려해보시면 좋을 것 같습니다.
예를 들어 노드가 없을 때 0을 반환하고,
1 + max(leftDepth, rightDepth) 형태로 계산하면
각 재귀 호출이 자신의 서브트리 깊이를 자연스럽게 return 값으로 전달하게 됩니다.
이렇게 하면 재귀 함수의 매개변수를 줄일 수 있어요! 😊
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.
그러네요! 말씀해주신 방법으로도 풀어봐야겠어요! 조언 감사드려요 🙇♂️
| function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null { | ||
| if (!list1 || !list2) { | ||
| return list1 ?? list2; | ||
| } | ||
| function merge(node1: ListNode | null, node2: ListNode | null) { | ||
| if (!node1 || !node2) { | ||
| return node1 ?? node2; | ||
| } | ||
| if (node1.val < node2.val) { | ||
| node1.next = merge(node1.next, node2); | ||
| } | ||
| if (node1.val >= node2.val) { | ||
| node2.next = merge(node1, node2.next); | ||
| } | ||
|
|
||
| return node1.val < node2.val ? node1 : node2; | ||
| } | ||
| return merge(list1, list2); | ||
| } |
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.
재귀로도 이렇게 깔끔한 풀이가 가능하네요! 😮
node1.val < node2.val 기준으로 next를 재귀적으로 이어붙이는 구조가 명확하게 보여서 로직이 한눈에 보이네요!!
저는 재귀가 약해서 이해하는 데 조금 시간이 걸렸는데요 😅
다음에는 참고해서 풀어봐야겠어요!
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.
저도 재귀는 아직 구현하면서도 햇갈려서 조건주는게 많이 어색한것같아요 ㅠ 더 많이 풀어봐야할것같아요!
ys-han00
left a comment
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.
금요일이다 보니 미리 승인해두겠습니다!
답안 제출 문제
작성자 체크 리스트
In Review로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!