-
-
Notifications
You must be signed in to change notification settings - Fork 245
[yyyyyyyyyKim] WEEK 15 solutions #1655
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 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c34d9b3
subtree-of-another-tree solution
yyyyyyyyyKim 9aede3e
subtree-of-another-tree solution
yyyyyyyyyKim 4dd47c6
longest-palindromic-substring solution
yyyyyyyyyKim 1f174be
construct-binary-tree-from-preorder-and-inorder-traversal solution
yyyyyyyyyKim 9250b9e
rotate-image solution
yyyyyyyyyKim e5260e7
alien-dictionary solution
yyyyyyyyyKim 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,46 @@ | ||
""" | ||
LeetCode 572. Subtree of Another Tree | ||
https://leetcode.com/problems/subtree-of-another-tree/ | ||
|
||
summary: | ||
root 트리 안에 subRoot 트리와 동일한 구조 & 값을 가진 서브트리가 있는지 확인 | ||
""" | ||
|
||
# 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: | ||
|
||
# DFS | ||
# 시간복잡도 O(n*m) : n = root트리 노드 개수, m = subRoot트리 노드 개수 | ||
# 공간복잡도 O(h1+h2) : h1 = root의 최대높이(최악O(n)), h2 = subRoot의 최대높이(최악O(m)) | ||
# 두 트리의 구조와 값이 같은지 확인 | ||
def isSameTree(node1, node2): | ||
# 둘 다 없으면 True | ||
if not node1 and not node2: | ||
return True | ||
# 둘 중 하나만 없으면 False | ||
if not node1 or not node2: | ||
return False | ||
# 현재 노드 값이 다르면 False | ||
if node1.val != node2.val: | ||
return False | ||
|
||
# root 트리랑 subRoot 트리의 양쪽 서브트리 재귀 탐색 | ||
return isSameTree(node1.left, node2.left) and isSameTree(node1.right, node2.right) | ||
|
||
# root 트리를 DFS로 돌면서 subRoot와 같은지 확인 | ||
def dfs(node): | ||
if not node: | ||
return False | ||
if isSameTree(node, subRoot): | ||
return True | ||
|
||
# root 트리의 양쪽 서브트리 탐색 | ||
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.
return False인 값을 or로 연결해서 작성할 수 있을 것 같은데
이 부분은 가독성을 위해 if 문을 두 개로 나눠 작성하신 건지 궁금합니다.
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.
조건이 좀 더 명확하게 보이고 가독성을 높이기 위해 나눠서 작성했습니다~