From ab6622cc01396a21fcde2e5a1cdcc3a27cdd5543 Mon Sep 17 00:00:00 2001 From: jamiebase <100mgml@gmail.com> Date: Sat, 23 May 2026 17:41:32 +0900 Subject: [PATCH 1/4] Add solution for checking if two binary trees are the same --- same-tree/jamiebase.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 same-tree/jamiebase.py diff --git a/same-tree/jamiebase.py b/same-tree/jamiebase.py new file mode 100644 index 0000000000..08b0bd96e1 --- /dev/null +++ b/same-tree/jamiebase.py @@ -0,0 +1,26 @@ +""" +# Approach +재귀를 이용해서 왼쪽, 오른쪽 서브트리의 값을 비교합니다 + +# Complexity +- Time complexity: 노드 개수 N일 때, O(N) +- Space complexity: 트리 높이 H 일때, O(H) +""" + + +# 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 isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: + if not p and not q: + return True + if not p or not q: + return False + if p.val != q.val: + return False + + return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) From 555369f5ac0d430f694fdbe8d67306adbea3f7ef Mon Sep 17 00:00:00 2001 From: jamiebase <100mgml@gmail.com> Date: Tue, 26 May 2026 12:04:35 +0900 Subject: [PATCH 2/4] add: implement lowest common ancestor for binary search tree --- .../jamiebase.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 lowest-common-ancestor-of-a-binary-search-tree/jamiebase.py diff --git a/lowest-common-ancestor-of-a-binary-search-tree/jamiebase.py b/lowest-common-ancestor-of-a-binary-search-tree/jamiebase.py new file mode 100644 index 0000000000..a91622b097 --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/jamiebase.py @@ -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 From 43860f8ab10dcf48207c29e60718e265918ad541 Mon Sep 17 00:00:00 2001 From: jamiebase <100mgml@gmail.com> Date: Sun, 7 Jun 2026 16:56:58 +0900 Subject: [PATCH 3/4] Implement solution for House Robber II problem --- house-robber-ii/jamiebase.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 house-robber-ii/jamiebase.py diff --git a/house-robber-ii/jamiebase.py b/house-robber-ii/jamiebase.py new file mode 100644 index 0000000000..131e38d31f --- /dev/null +++ b/house-robber-ii/jamiebase.py @@ -0,0 +1,32 @@ +""" +# Intuition +집이 원형으로 놓여있기 때문에 첫 번째 집을 터는 경우, 안 터는 경우일 때로 나누어 해를 구하고, +두 경우에서 최댓값 해를 구한다. + +# Complexity +n은 nums의 길이라고 할 때, +- Time complexity: O(N) +- Space complexity: O(N) + +""" + + +class Solution: + def rob(self, nums: list[int]) -> int: + n = len(nums) + if n == 1: + return nums[0] + + # 첫 번째 집을 터는 경우 + dp1 = [0] * n + dp1[0] = dp1[1] = nums[0] + for i in range(2, n - 1): + dp1[i] = max(dp1[i - 2] + nums[i], dp1[i - 1]) + + # 첫 번째 집을 안 터는 경우 + dp2 = [0] * n + dp2[1] = nums[1] + for j in range(2, n): + dp2[j] = max(dp2[j - 2] + nums[j], dp2[j - 1]) + + return max(max(dp1), max(dp2)) From 9e6973a44c75f96a40f2104c98db05b04b36b070 Mon Sep 17 00:00:00 2001 From: jamiebase <100mgml@gmail.com> Date: Thu, 11 Jun 2026 21:41:49 +0900 Subject: [PATCH 4/4] Implement solution for Subtree of Another Tree problem --- subtree-of-another-tree/jamiebase.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 subtree-of-another-tree/jamiebase.py diff --git a/subtree-of-another-tree/jamiebase.py b/subtree-of-another-tree/jamiebase.py new file mode 100644 index 0000000000..6a764abc17 --- /dev/null +++ b/subtree-of-another-tree/jamiebase.py @@ -0,0 +1,28 @@ +""" +# Approach +매 노드마다 같은 Tree인지 비교합니다. + +# Complexity +- Time complexity: 양 트리의 노드를 하나씩 다 비교해야 하므로 최악의 경우 O(N*M) +- Space complexity: root 트리 높이가 H라고 할 때 O(H)만큼 재귀 스택이 쌓임 +""" + + +class Solution: + def isSameTree(self, a, b): + if not a and not b: + return True + if not a or not b: + return False + if a.val != b.val: + return False + return self.isSameTree(a.left, b.left) and self.isSameTree(a.right, b.right) + + def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: + if not root: + return False + + if self.isSameTree(root, subRoot): + return True + + return self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot)