[parkhojeong] WEEK 13 Solutions - #2865
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
🏷️ 알고리즘 패턴 분석
kth-smallest-element-in-a-bst/parkhojeong.py
# 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]- 패턴: Binary Search, Hash Map / Hash Set, Binary Search
- 설명: 주어진 코드는 이진 탐색 트리의 중위 순회를 통해 정렬된 결과를 얻고, 그 중 k-1번째를 반환합니다. 중위 순회 자체는 트리 구조를 다루지만 핵심 아이디어는 이진 탐색 트리의 정렬 특성을 이용하는 패턴으로 볼 수 있습니다.
📊 시간/공간 복잡도 분석
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 중위 순회를 이용해 모든 노드를 방문하고 배열에 저장하므로 시간은 선형이며 추가 공간은 저장된 노드 수에 비례한다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
Contributor
📊 parkhojeong 님의 학습 현황이번 주 제출 문제
누적 학습 요약
문제 풀이 현황
🤖 이 댓글은 GitHub App을 통해 자동으로 작성되었습니다. 🔢 API 사용량 (gpt-5-nano)
|
Contributor
There was a problem hiding this comment.
🏷️ 알고리즘 패턴 분석
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- 패턴: Binary Search, Depth-First Search, Hash Map / Hash Set
- 설명: BST에서 특정 값의 위치를 찾는 방식으로 루트에서 자식 방향을 탐색하며, 두 노드의 LCA를 찾기 위해 경로를 추적하고 교집합을 확인하는 흐름이 주로 Binary Search와 DFS의 혼합 형태로 보이며, 경로 비교를 위해 해시 집합을 사용합니다.
📊 시간/공간 복잡도 분석
| 복잡도 | |
|---|---|
| Time | O(h) |
| Space | O(h) |
피드백: 스택 기반으로 순회하며 검색하는 방식이 필요하지 않다. 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
답안 제출 문제
작성자 체크 리스트
In Review로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!