-
-
Notifications
You must be signed in to change notification settings - Fork 244
[wozlsla] WEEK 12 solution #1941
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
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,49 @@ | ||
from typing import Optional | ||
|
||
""" | ||
# Intuition | ||
어떻게 순서가 정의되지? | ||
순회 -> 어떤 구조? | ||
비교 | ||
|
||
# Approach | ||
트리가 동일하려면 ? | ||
1. 구조 비교 | ||
2. 값 비교 | ||
|
||
트리 동시 탐색 | ||
- 두 노드가 모두 null (구조) -> True | ||
- 둘 중 하나만 null (구조) -> False | ||
- 두 노드가 모두 값이 있음 (값) -> True/False | ||
|
||
# Complexity | ||
시간 복잡도 | ||
- O(N) | ||
공간 복잡도 | ||
- (재귀) 전위 순회: 콜 스택 -> O(H) | ||
""" | ||
|
||
|
||
# Definition for a binary tree node. | ||
class TreeNode: | ||
def __init__(self, val=0, left=None, right=None): | ||
self.val = val | ||
self.left = left | ||
self.right = right | ||
|
||
|
||
class Solution: | ||
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: | ||
|
||
# 종결 조건 1: 두 노드가 모두 null이면 구조가 동일함 | ||
if p is None and q is None: | ||
return True | ||
|
||
# 종료 조건 2: | ||
# - 구조 불일치 | ||
# - 값 불일치 | ||
if p is None or q is None or p.val != q.val: | ||
return False | ||
|
||
# (재귀) 자식 비교 | ||
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) |
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.
사실상
H=O(N)
이군요. Balanced Tree 제한이 없으니까요. 만일 있었다면 반대로 시간 복잡도가O(H)
였겠고요.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.
오.. 그렇네요. 또 하나 배워갑니다, 피드백 감사합니다!