From 84269387f2168f7979747f1c65d7769a16b724d7 Mon Sep 17 00:00:00 2001 From: Yuseok Jo Date: Fri, 2 Oct 2026 20:38:44 +0900 Subject: [PATCH 1/5] subtree of another tree solution --- subtree-of-another-tree/yuseok89.py | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 subtree-of-another-tree/yuseok89.py diff --git a/subtree-of-another-tree/yuseok89.py b/subtree-of-another-tree/yuseok89.py new file mode 100644 index 0000000000..bd5905e55e --- /dev/null +++ b/subtree-of-another-tree/yuseok89.py @@ -0,0 +1,42 @@ +# TC: O(N) +# SC: O(N) +class Solution: + def isSubtree(self, root: TreeNode | None, subRoot: TreeNode | None) -> bool: + + def isSameTree(p, q): + if not p and not q: + return True + if not p or not q or p.val != q.val: + return False + + return isSameTree(p.left, q.left) and isSameTree(p.right, q.right) + + def get_size(node): + if not node: + return 0 + return 1 + get_size(node.left) + get_size(node.right) + + target_size = get_size(subRoot) + is_match = False + + def check_tree(node): + nonlocal is_match + + if not node or is_match: + return 0 + + left_size = check_tree(node.left) + right_size = check_tree(node.right) + + current_size = 1 + left_size + right_size + + if current_size == target_size: + if isSameTree(node, subRoot): + is_match = True + + return current_size + + check_tree(root) + + return is_match + From 1c9597abe323b2ae6aeb846f8ecc877706108612 Mon Sep 17 00:00:00 2001 From: Yuseok Jo Date: Fri, 2 Oct 2026 20:40:09 +0900 Subject: [PATCH 2/5] construct binary tree from preorder and inorder traversal solution --- .../yuseok89.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/yuseok89.py diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/yuseok89.py b/construct-binary-tree-from-preorder-and-inorder-traversal/yuseok89.py new file mode 100644 index 0000000000..4fc76c7eac --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/yuseok89.py @@ -0,0 +1,33 @@ +# TC: O(N) +# SC: O(N) +# 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 buildTree(self, preorder: list[int], inorder: list[int]) -> TreeNode | None: + + inorder_map = {val: idx for idx, val in enumerate(inorder)} + preorder_idx = 0 + + def build(left: int, right: int) -> TreeNode | None: + nonlocal preorder_idx + + if left > right: + return None + + root_val = preorder[preorder_idx] + root = TreeNode(root_val) + preorder_idx += 1 + + mid = inorder_map[root_val] + + root.left = build(left, mid - 1) + root.right = build(mid + 1, right) + + return root + + return build(0, len(inorder) - 1) + From efbc24709246a96227b754e2d31f5b7a645faae2 Mon Sep 17 00:00:00 2001 From: Yuseok Jo Date: Fri, 2 Oct 2026 20:41:12 +0900 Subject: [PATCH 3/5] longest palindromic substring solution --- longest-palindromic-substring/yuseok89.py | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 longest-palindromic-substring/yuseok89.py diff --git a/longest-palindromic-substring/yuseok89.py b/longest-palindromic-substring/yuseok89.py new file mode 100644 index 0000000000..864f4f42c4 --- /dev/null +++ b/longest-palindromic-substring/yuseok89.py @@ -0,0 +1,29 @@ +# TC: O(N) +# SC: O(1) +class Solution: + def longestPalindrome(self, s: str) -> str: + + n = len(s) + max_len, left = 1, 0 + + for mid in range(n): + l, r = mid, mid + + while l >= 0 and r < n and s[l] == s[r]: + l -= 1 + r += 1 + + if max_len < r - l - 1: + max_len, left = r - l - 1, l + 1 + + l, r = mid, mid + 1 + + while l >= 0 and r < n and s[l] == s[r]: + l -= 1 + r += 1 + + if max_len < r - l - 1: + max_len, left = r - l - 1, l + 1 + + return s[left:left + max_len] + From be07fadf1af295ecc17bda8897d29edcb673f16d Mon Sep 17 00:00:00 2001 From: Yuseok Jo Date: Fri, 2 Oct 2026 20:43:46 +0900 Subject: [PATCH 4/5] rotate image solution --- rotate-image/yuseok89.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 rotate-image/yuseok89.py diff --git a/rotate-image/yuseok89.py b/rotate-image/yuseok89.py new file mode 100644 index 0000000000..269b13912c --- /dev/null +++ b/rotate-image/yuseok89.py @@ -0,0 +1,25 @@ +# TC: O(N^2) +# SC: O(1) +class Solution: + def rotate(self, matrix: list[list[int]]) -> None: + """ + Do not return anything, modify matrix in-place instead. + """ + + n = len(matrix) + + for k in range(0, n // 2): + + y11, x11 = k, k + y12, x12 = k, n - 1 - k + y21, x21 = n - 1 - k, k + y22, x22 = n - 1 - k, n - 1 - k + + for _ in range(0, n - 1 - k * 2): + matrix[y11][x11], matrix[y21][x21], matrix[y22][x22], matrix[y12][x12] = matrix[y21][x21], matrix[y22][x22], matrix[y12][x12], matrix[y11][x11] + + x11 += 1 + y12 += 1 + x22 -= 1 + y21 -= 1 + From 58edc7bfa13372db1ec0e1caedb31d8d6d0f06bd Mon Sep 17 00:00:00 2001 From: Yuseok Jo Date: Fri, 2 Oct 2026 20:49:15 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=EB=B3=B5=EC=9E=A1=EB=8F=84=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- longest-palindromic-substring/yuseok89.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/longest-palindromic-substring/yuseok89.py b/longest-palindromic-substring/yuseok89.py index 864f4f42c4..94ea47898b 100644 --- a/longest-palindromic-substring/yuseok89.py +++ b/longest-palindromic-substring/yuseok89.py @@ -1,4 +1,4 @@ -# TC: O(N) +# TC: O(N^2) # SC: O(1) class Solution: def longestPalindrome(self, s: str) -> str: