-
-
Notifications
You must be signed in to change notification settings - Fork 304
[dylan-jung] WEEK 02 solutions #2047
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.
안녕하세요! 전체적으로 코드가 깔끔해서 이해하기 쉬웠어요.
다만 이 코드에서는 추가적으로 null 노드에 대한 base case를 체크해 주면 더 좋을 것 같아요.
bool isValidBST(TreeNode* root) {
if (!root) return true;
return dfs(root, -(1L << 32), 1L << 32);
}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.
또 한 가지는, dfs 함수의 맨 처음에서
if (!(minVal < root->val && root->val < maxVal)) return false;이 조건을 이미 검사하고 있기 때문에, 이 시점에서는 항상
minVal < root->val < maxVal 가 보장됩니다.
그래서 min((long)root->val, maxVal)는 실제로는 항상 root->val과 같고,
마찬가지로 right 쪽에서도 max((long)root->val, minVal)는 항상 root->val과 같아서
if (root->left) {
isValid = isValid && dfs(root->left, minVal, root->val);
}
if (root->right) {
isValid = isValid && dfs(root->right, root->val, maxVal);
}처럼 수정해도 동일하게 동작을 하게 되는거 같아요.
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 하나 이상을 반드시 검토를 해주셔야 합니다!