-
Notifications
You must be signed in to change notification settings - Fork 126
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
[croucs] WEEK 2 #714
[croucs] WEEK 2 #714
Conversation
class Solution: | ||
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]: | ||
if len(preorder) == 0: | ||
return None | ||
if len(preorder) == 1: | ||
return TreeNode(preorder[0]) | ||
root = TreeNode(preorder[0]) | ||
|
||
split_idx = inorder.index(preorder[0]) | ||
left_inorder = inorder[:split_idx] | ||
right_inorder = inorder[split_idx+1:] | ||
left_preorder = preorder[1:len(left_inorder)+1] | ||
right_preorder = preorder[len(left_inorder)+1:] | ||
root.left = self.buildTree(left_preorder, left_inorder) | ||
root.right = self.buildTree(right_preorder, right_inorder) | ||
return root |
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.
inorder과 preorder의 관계를 사용하여 dfs 풀이도 참조하시면 문제 이해에 더 도움이 되시지 않을까 싶습니다!
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.
2주차 풀이도 고생 많으셨습니다!
시간, 공간 복잡도도 잘 정리해주셔서 이해하기 쉬었네요.
고생하셨습니다!
답안 제출 문제
체크 리스트
In Review
로 설정해주세요.