Skip to content
Open
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
22 changes: 22 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/jamiebase.py
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Binary Search
  • 설명: 이 코드는 이진 탐색 트리의 특성을 이용하여 값 비교로 공통 조상을 찾는 방식으로, 이진 탐색 패턴에 속합니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(H)
Space O(1)

피드백: 트리의 높이 H에 비례하는 시간 복잡도를 가지며, 반복문을 통해 상위 노드로 이동하므로 공간 복잡도는 상수입니다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
# Approach
BST 의 특징을 이용해서 값의 대소 비교로 공통 조상을 찾습니다.

# Complexity
- Time complexity: 트리의 높이 H일때, O(H)
- Space complexity: O(1)
"""


class Solution:
def lowestCommonAncestor(
self, root: "TreeNode", p: "TreeNode", q: "TreeNode"
) -> "TreeNode":
cur = root
while cur:
if p.val < cur.val and q.val < cur.val:
cur = cur.left
elif p.val > cur.val and q.val > cur.val:
cur = cur.right
else:
return cur
Loading