Skip to content

[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 2 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions alien-dictionary/shinsj4653.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
[문제풀이]
# Inputs

# Outputs

# Constraints

# Ideas

[회고]

"""


Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
[문제풀이]
# Inputs

# Outputs

# Constraints

# Ideas

[회고]

"""


15 changes: 15 additions & 0 deletions longest-palindromic-substring/shinsj4653.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
[문제풀이]
# Inputs

# Outputs

# Constraints

# Ideas

[회고]

"""


15 changes: 15 additions & 0 deletions rotate-image/shinsj4653.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
[문제풀이]
# Inputs

# Outputs

# Constraints

# Ideas

[회고]

"""


90 changes: 90 additions & 0 deletions subtree-of-another-tree/shinsj4653.py
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:
Copy link
Contributor

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 체크가 먼저 필요

if r.val != sr.val:
print('not same, exit compare')
return False

elif r.val == sr.val == None:
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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)