-
-
Notifications
You must be signed in to change notification settings - Fork 202
[shinsj4653] Week 15 Solutions #1667
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,15 @@ | ||
""" | ||
[문제풀이] | ||
# Inputs | ||
|
||
# Outputs | ||
|
||
# Constraints | ||
|
||
# Ideas | ||
|
||
[회고] | ||
|
||
""" | ||
|
||
|
15 changes: 15 additions & 0 deletions
15
construct-binary-tree-from-preorder-and-inorder-traversal/shinsj4653.py
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,15 @@ | ||
""" | ||
[문제풀이] | ||
# Inputs | ||
|
||
# Outputs | ||
|
||
# Constraints | ||
|
||
# Ideas | ||
|
||
[회고] | ||
|
||
""" | ||
|
||
|
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,15 @@ | ||
""" | ||
[문제풀이] | ||
# Inputs | ||
|
||
# Outputs | ||
|
||
# Constraints | ||
|
||
# Ideas | ||
|
||
[회고] | ||
|
||
""" | ||
|
||
|
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,15 @@ | ||
""" | ||
[문제풀이] | ||
# Inputs | ||
|
||
# Outputs | ||
|
||
# Constraints | ||
|
||
# Ideas | ||
|
||
[회고] | ||
|
||
""" | ||
|
||
|
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,90 @@ | ||
""" | ||
[문제풀이] | ||
# Inputs | ||
root, subRoot | ||
|
||
# Outputs | ||
subRoot의 트리가 root 트리의 서브 트리인지에 대한 여부 | ||
|
||
# Constraints | ||
- The number of nodes in the root tree is in the range [1, 2000]. | ||
- The number of nodes in the subRoot tree is in the range [1, 1000]. | ||
- -10^4 <= root.val <= 10^4 | ||
- -10^4 <= subRoot.val <= 10^4 | ||
|
||
# Ideas | ||
정확히 구조랑 노드 개수도 같아야 함 | ||
-> subRoot의 root 노드 찾으면, 탐색 시작 | ||
-> 탐색 끝나고 나서의 node 수 반환 | ||
탐색 도중에 node.value 다르면 false 반환 | ||
|
||
[회고] | ||
|
||
""" | ||
# 1차 제출 코드 | ||
# 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 isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: | ||
|
||
def compare(r, sr): | ||
print('start compare') | ||
if r is not None and sr.val is not None: | ||
if r.val != sr.val: | ||
print('not same, exit compare') | ||
return False | ||
|
||
elif r.val == sr.val == None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. r.val != sr.val을 이미 처리했기 때문에 작성하지 않아도됨 |
||
print('same, exit compare') | ||
return True | ||
|
||
else: | ||
print('continue compare') | ||
return compare(r.left, sr.left) or compare(r.right, sr.right) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 두 트리가 완전히 동일한지 비교하는 함수에서 or 사용은 부적절 |
||
|
||
return True | ||
|
||
def dfs(cur): | ||
if cur.val != subRoot.val: | ||
print('going left in root') | ||
dfs(cur.left) | ||
|
||
print('going right in root') | ||
dfs(cur.right) | ||
|
||
else: | ||
print('begin compare') | ||
return compare(cur, subRoot) | ||
return | ||
|
||
return dfs(root) | ||
|
||
# gpt 수정 코드 | ||
|
||
class Solution: | ||
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: | ||
|
||
def isSameTree(s, t): | ||
if not s and not t: | ||
return True | ||
if not s or not t: | ||
return False | ||
if s.val != t.val: | ||
return False | ||
return isSameTree(s.left, t.left) and isSameTree(s.right, t.right) | ||
|
||
def dfs(node): | ||
if not node: | ||
return False | ||
if isSameTree(node, subRoot): | ||
return True | ||
# 아래 둘 중 하나라도 True면 즉시 return True | ||
return dfs(node.left) or dfs(node.right) | ||
|
||
return dfs(root) | ||
|
||
|
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.
sr.val 접근 전에 sr is not None 체크가 먼저 필요