-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hyer0705] WEEK 15 solutions #1962
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
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.
수고하셨습니다!
| const isSameTree = (root: TreeNode | null, subRoot: TreeNode | null): boolean => { | ||
| if (!root && !subRoot) return true; | ||
| if (!root || !subRoot) return false; | ||
| if (root.val !== subRoot.val) return false; | ||
|
|
||
| return isSameTree(root.left, subRoot.left) && isSameTree(root.right, subRoot.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.
isSameTree 구현을 정석대로 잘 구현해주신 것 같습니다 ! ts 에서는 !root 가 Null 여부를 판단해주는 것 같아서 또 배워갑니다 ㅎㅎ
마지막에 subRoot 그 자체로 포함하는지, 그리고 left, right 한번씩 넣어주어 자식들을 재귀형식으로 탐색하는 방법으로 잘 구현해주신 것 같습니다
| root.left = build(preStart + 1, preStart + leftSize, inStart, rootIdx - 1); | ||
| root.right = build(preStart + leftSize + 1, preEnd, rootIdx + 1, inEnd); |
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.
이 부분에서 어떤 기준으로 +1 을 하고 -1 을 하셨는지 여쭤봐도 될까요 ? 문제를 읽기만 하고 풀지는 못해서 여쭤봅니다
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.
+1, -1을 하는 이유는 인덱스 범위를 조정하기 위함입니다.
먼저 기본 개념부터:
- preorder[0]은 항상 현재 구간의 루트 노드
- inorder에서 루트의 위치(rootIdx)를 찾으면,
왼쪽은 왼쪽 서브트리, 오른쪽은 오른쪽 서브트리
예시: preorder = [4, 11, 56, 78], inorder = [11, 4, 56, 78]
- root = 4 (preorder[0])
- rootIdx = 1 (inorder에서 4의 위치)
- 왼쪽 서브트리 크기(leftSize) = 1 (rootIdx - inStart = 1 - 0)
왼쪽 서브트리의 범위:
preorder: [11] → 인덱스 [1, 1]
inorder: [11] → 인덱스 [0, 0]
root.left = build(preStart + 1, preStart + leftSize, inStart, rootIdx - 1)
- preStart + 1: 루트(preorder[0])를 건너뛰고 그 다음부터 시작
- preStart + leftSize: 왼쪽 서브트리 크기만큼만 포함 (1 + 1 = 2, 하지만 범위는 [1,1])
- inStart: 왼쪽 서브트리는 inorder의 시작점부터 시작하므로 그대로
- rootIdx - 1: 루트 직전까지가 왼쪽 서브트리의 끝
오른쪽 서브트리도 같은 논리로:
- preStart + leftSize + 1: 루트 + 왼쪽 서브트리를 건너뛴 다음부터
- rootIdx + 1: 루트 다음부터가 오른쪽 서브트리의 시작
| const [s1, e1] = expandAroundCenter(s, i, i); | ||
| const [s2, e2] = expandAroundCenter(s, i, i + 1); | ||
|
|
||
| const odd = e1 - s1 + 1; | ||
| const even = e2 - s2 + 1; |
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.
위의 expandAroundCenter 함수의 while 부분에서는 left 와 right 로 포인터를 잡아서 서로 길이가 같을때까지 루프를 도는 것 까지는 이해를 하였는데
여기 For 문에서의 s, i, i 와 s, i, i+1 을 사용하신 부분은 어떤 의도로 사용하신지가 궁금합니다 ㅎㅎ
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.
Palindrome이 홀수 길이일 경우, 짝수 길이일 경우에 대해서 체크하는 코드입니다.
예시) "토마토", "라랄랄라"
홀수의 경우는 left, right 포인터가 동일하면 되지만 짝수의 경우는 가운데가 두 개여야 하므로 s, i, i+1을 사용했습니다!
expandAroundCenter() 함수는 넘겨준 파라미터인 left와 right에서 시작해서 각각 왼쪽 오른쪽 문자가 같은지 확인해서 펠린드롬을 확인합니다. left, right가 가운데로 설정하는걸 홀수일 때, 짝수일 때 확인해보기 위해서 s, i, i 를 넣어 호출(홀수) 하고 s, i, i+1을 넣어 호출(짝수)하였습니다.
답안 제출 문제
작성자 체크 리스트
In Review로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!