-
-
Notifications
You must be signed in to change notification settings - Fork 195
[HodaeSsi] Week 10 #1009
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
[HodaeSsi] Week 10 #1009
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,28 @@ | ||
from typing import Optional | ||
|
||
class TreeNode: | ||
def __init__(self, val=0, left=None, right=None): | ||
self.val = val | ||
self.left = left | ||
self.right = right | ||
|
||
# 시간 복잡도 : O(n) | ||
# 공간 복잡도 : O(n) | ||
class Solution: | ||
# 오른쪽부터 탐색하는 전위 순회 구현 | ||
def invertPreOrder(self, src: TreeNode, des: TreeNode) -> None: | ||
des.val = src.val | ||
if src.right: | ||
des.left = TreeNode() | ||
self.invertPreOrder(src.right, des.left) | ||
if src.left: | ||
des.right = TreeNode() | ||
self.invertPreOrder(src.left, des.right) | ||
|
||
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: | ||
if not root: | ||
return None | ||
des = TreeNode() | ||
self.invertPreOrder(root, des) | ||
return des | ||
|
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,44 @@ | ||
from typing import List | ||
|
||
# 시간 복잡도 : O(log n) (* 최대 세번의 이진 탐색 수행) | ||
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. 제 생각엔 이진 탐색을 세번 수행하는 것보다는, |
||
# 공간 복잡도 : O(1) | ||
class Solution: | ||
def find_pivot_idx(self, src_list: List[int], start_idx: int, end_idx: int) -> int: | ||
if start_idx == end_idx: | ||
return start_idx | ||
mid_idx = (start_idx + end_idx) // 2 | ||
if src_list[mid_idx] > src_list[end_idx]: | ||
return self.find_pivot_idx(src_list, mid_idx + 1, end_idx) | ||
else: | ||
return self.find_pivot_idx(src_list, start_idx, mid_idx) | ||
|
||
|
||
def find_target_idx(self, src_list: List[int], start_idx: int, end_idx: int, target) -> int: | ||
if start_idx > end_idx: | ||
return -1 | ||
if start_idx == end_idx: | ||
if src_list[start_idx] == target: | ||
return start_idx | ||
else: | ||
return -1 | ||
|
||
mid_idx = (start_idx + end_idx) // 2 | ||
if src_list[mid_idx] == target: | ||
return mid_idx | ||
elif src_list[mid_idx] > target: | ||
return self.find_target_idx(src_list, start_idx, mid_idx - 1, target) | ||
else: | ||
return self.find_target_idx(src_list, mid_idx + 1, end_idx, target) | ||
|
||
|
||
def search(self, nums: List[int], target: int) -> int: | ||
if nums[0] > nums[len(nums) - 1]: | ||
pivot_idx = self.find_pivot_idx(nums, 0, len(nums) - 1) | ||
left = self.find_target_idx(nums, 0, pivot_idx - 1, target) | ||
if left != -1: | ||
return left | ||
else: | ||
return self.find_target_idx(nums, pivot_idx, len(nums) - 1, target) | ||
else: | ||
return self.find_target_idx(nums, 0, len(nums) - 1, target) | ||
|
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.
input으로 주어진 root를 변경하지 않기 위해 의도적으로 추가적인 노드 des를 만들어서 반환하신 건가요?