diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/hi-rachel.py b/construct-binary-tree-from-preorder-and-inorder-traversal/hi-rachel.py new file mode 100644 index 000000000..5a883a7a8 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/hi-rachel.py @@ -0,0 +1,36 @@ +""" +https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ +두 개의 정수 배열 preorder와 inoder가 주어졌을 때 이진 트리를 구성하고 반환하시오. +preorder = 전위 순회 : 루트 -> 왼쪽 서브트리 -> 오른쪽 서브트리 +inorder = 중위 순회 : 왼쪽 서브트리 -> 루트 -> 오른쪽 서브트리 + +TC: O(n), n: 노드 수 +SC: O(n) +""" + +from typing import Optional, List + +# 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]) -> Optional[TreeNode]: + if not preorder or not inorder: + return None + + # 1. preorder의 첫 값이 root + root_val = preorder[0] + root = TreeNode(root_val) + + # 2. inorder에서 root 위치 찾기 + root_index = inorder.index(root_val) + + # 3. 왼쪽, 오른쪽 나눠서 재귀적으로 연결 + root.left = self.buildTree(preorder[1:root_index + 1], inorder[:root_index]) + root.right = self.buildTree(preorder[root_index + 1:], inorder[root_index + 1:]) + + return root diff --git a/longest-palindromic-substring/hi-rachel.py b/longest-palindromic-substring/hi-rachel.py new file mode 100644 index 000000000..1311bc335 --- /dev/null +++ b/longest-palindromic-substring/hi-rachel.py @@ -0,0 +1,38 @@ +""" +https://leetcode.com/problems/longest-palindromic-substring/ + +문자열 s가 주어졌을 때, 가장 긴 팰린드롬 부분 문자열을 찾아서 반환하는 함수를 작성해라. + +문제 풀이 + +1. 문자열 `s`의 모든 인덱스 `i`를 기준으로, +2. 두 종류의 중심에서 팰린드롬을 확장해 본다: + - 홀수 길이: `s[i]`를 중심으로 좌우 확장 (`i, i`) + - 짝수 길이: `s[i]`와 `s[i+1]`을 중심으로 좌우 확장 (`i, i+1`) +3. 각 중심에서 while문으로 `s[left] == s[right]`인 동안 확장 +4. 가장 긴 팰린드롬 문자열을 계속 업데이트 +5. 최종적으로 가장 긴 팰린드롬을 반환한다 + +TC: O(n^2) +SC: O(1) +""" + + +class Solution: + def longestPalindrome(self, s: str) -> str: + def expand(left: int, right: int) -> str: + while left >= 0 and right < len(s) and s[left] == s[right]: + left -= 1 + right += 1 + + return s[left + 1 : right] + + res = "" + for i in range(len(s)): + temp1 = expand(i, i) # 홀수 길이 팰린드롬 + temp2 = expand(i, i+1) # 짝수 길이 팰린드롬 + if len(temp1) > len(res): + res = temp1 + if len(temp2) > len(res): + res = temp2 + return res diff --git a/subtree-of-another-tree/hi-rachel.py b/subtree-of-another-tree/hi-rachel.py new file mode 100644 index 000000000..7ab9c463e --- /dev/null +++ b/subtree-of-another-tree/hi-rachel.py @@ -0,0 +1,43 @@ +""" +https://leetcode.com/problems/subtree-of-another-tree/description/ + +두 개의 이진 트리 root와 subRoot의 루트(최상위 노드)가 주어졌을 때, +root의 하위 트리 중 subRoot와 동일한 구조와 값이 있는 경우 참을 반환하고 그렇지 않은 경우 거짓을 반환 + +TC: O(n * m), n: root의 노드 수, m: subRoot의 노드 수 +SC: O(n + m) +""" + +from typing import Optional + +# 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 isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: + if not root: + return False + if not subRoot: + return True + + def isSameTree(p, q): + # 둘 중 하나라도 None 이면, + if not p or not q: + # 둘 다 None 일 때만 True + return not p and not q + # 같이 다르면 False + if p.val != q.val: + return False + # 왼쪽과 오른쪽 서브트리를 재귀적으로 비교 + return isSameTree(p.left, q.left) and isSameTree(p.right, q.right) + + # root에서 시작해 subRoot와 동일한 구조의 트리인지 확인 + if isSameTree(root, subRoot): + return True + + # 왼쪽 또는 오른쪽 서브트리 중 하나라도 subRoot와 같은 트리가 있는지 재귀 탐색 + return self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot)