-
-
Notifications
You must be signed in to change notification settings - Fork 358
[parkhojeong] WEEK 13 Solutions #2865
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
+50
−0
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,21 @@ | ||
| # 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 kthSmallest(self, root: TreeNode | None, k: int) -> int: | ||
| arr = [] | ||
|
|
||
| def inorrder(node: TreeNode): | ||
| if node.left: | ||
| inorrder(node.left) | ||
|
|
||
| arr.append(node.val) | ||
|
|
||
| if node.right: | ||
| inorrder(node.right) | ||
|
|
||
| inorrder(root) | ||
| return arr[k - 1] |
29 changes: 29 additions & 0 deletions
29
lowest-common-ancestor-of-a-binary-search-tree/parkhojeong.py
|
Contributor
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. 🏷️ 알고리즘 패턴 분석lowest-common-ancestor-of-a-binary-search-tree/parkhojeong.py# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
def search(node: TreeNode, target: int):
stack = [node]
arr = []
while stack:
node = stack.pop()
arr.append(node)
if node.val > target:
stack.append(node.left)
elif node.val < target:
stack.append(node.right)
return arr
arr1 = set(search(root, p.val))
arr2 = search(root, q.val)
for node in arr2[::-1]:
if node in arr1:
return node
📊 시간/공간 복잡도 분석
피드백: 스택 기반으로 순회하며 검색하는 방식이 필요하지 않다. BST의 특성을 활용해 루트-노드 방향으로 비교하면 더 간결하고 안전하다. 개선 제안: 고려해볼 만한 대안: BST의 특성을 이용해 루트에서 시작해 p.val과 q.val의 대소를 비교하며 자식을 따라가면 된다. 불필요한 중복 탐색 제거.
|
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,29 @@ | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, x): | ||
| # self.val = x | ||
| # self.left = None | ||
| # self.right = None | ||
|
|
||
| class Solution: | ||
| def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': | ||
|
|
||
| def search(node: TreeNode, target: int): | ||
| stack = [node] | ||
| arr = [] | ||
| while stack: | ||
| node = stack.pop() | ||
| arr.append(node) | ||
| if node.val > target: | ||
| stack.append(node.left) | ||
| elif node.val < target: | ||
| stack.append(node.right) | ||
|
|
||
| return arr | ||
|
|
||
| arr1 = set(search(root, p.val)) | ||
| arr2 = search(root, q.val) | ||
|
|
||
| for node in arr2[::-1]: | ||
| if node in arr1: | ||
| return node |
Oops, something went wrong.
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.
🏷️ 알고리즘 패턴 분석
kth-smallest-element-in-a-bst/parkhojeong.py
📊 시간/공간 복잡도 분석
피드백: 중위 순회를 이용해 모든 노드를 방문하고 배열에 저장하므로 시간은 선형이며 추가 공간은 저장된 노드 수에 비례한다.
개선 제안: 현재 구현이 적절해 보입니다.