From 8eb44acf17c03a5a6f5aecb4dd4b1024ca37d46c Mon Sep 17 00:00:00 2001 From: hyerijung Date: Tue, 9 Jun 2026 19:17:58 +0900 Subject: [PATCH 1/3] subtree of another tree solution --- subtree-of-another-tree/hyeri0903.java | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 subtree-of-another-tree/hyeri0903.java diff --git a/subtree-of-another-tree/hyeri0903.java b/subtree-of-another-tree/hyeri0903.java new file mode 100644 index 0000000000..f91e00d1bb --- /dev/null +++ b/subtree-of-another-tree/hyeri0903.java @@ -0,0 +1,46 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public boolean isSubtree(TreeNode root, TreeNode subRoot) { + /** + 1.root 트리의 subtree가 subRoot tree 이면 true 아니면 false return + 2.constraints + - subRoot tree 가 존재하는데 거기에 자식 노드가 추가로 있으면 안됨 + - 자기 자신 root tree = subRoot tree 일수도 있음 + 3.solution + - 재귀함수로 subTree 찾고 subRoot 의 트리와 동일한지 검사 + - root 트리 노드 수 : n, subRoot 트리 노드 수 : m + - timeComplexity : O(nm) + */ + if(root == null) return false; + + //먼저 트리가 동일한지 체크 -> left subTree check -> right subTree check + return isSameTree(root, subRoot) + || isSubtree(root.left, subRoot) + || isSubtree(root.right, subRoot); + + } + private boolean isSameTree(TreeNode a, TreeNode b) { + //둘 다 null + if (a == null && b == null) return true; + //둘중 하나만 null + if (a == null || b == null) return false; + //값이 다른 경우 + if(a.val != b.val) return false; + //현재 값이 다르면 왼쪽끼리, 오른쪽끼리 비교, 모두 같아야 true return + return isSameTree(a.left, b.left) && isSameTree(a.right, b.right); + } +} From 8456ec65f2027f4636bd9fe122219f7cdff79100 Mon Sep 17 00:00:00 2001 From: hyerijung Date: Wed, 10 Jun 2026 17:53:51 +0900 Subject: [PATCH 2/3] longest-palindromic-substring solutions --- longest-palindromic-substring/hyeri0903.java | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 longest-palindromic-substring/hyeri0903.java diff --git a/longest-palindromic-substring/hyeri0903.java b/longest-palindromic-substring/hyeri0903.java new file mode 100644 index 0000000000..8d80a00e0f --- /dev/null +++ b/longest-palindromic-substring/hyeri0903.java @@ -0,0 +1,44 @@ +class Solution { + public String longestPalindrome(String s) { + /** + 1.문제: 가장 긴 팰린드롬 string 찾기. + 2.조건: + - s 문자열 길이 최소 1, 최대 1000 + - 답이 1개가 아닐수도있음. 여러개중 1개만 반환해도 ok + - s 는 digit, English letters 로 구성 + 3.풀이 + - stack 사용 -> TLE 발생 + - expand around center (각 중심에서 양쪽으로 확장하여 체크) + - 길이 = 1 이면 바로 return + Palindrome은 substring 기준이라 전체 문자열 길이와 무관하게 홀수/짝수 케이스가 모두 존재할 수 있기 때문에, 두 경우를 모두 확인해야 합니다. + + Time: O(n²) -> palindrome method 에서 최대 n 번 돌 수 있으므로 + Space: O(1) + */ + + int n = s.length(); + if(n == 1) return s; + //가운데 문자에서 시작 e.g "babad" n = 5, answer = "b" + String answer = ""; + + for(int i = 0; i < n; i++) { + //홀수 e.g "b". 중심이 1개 + String odd = palindrome(s, i, i); + //짝수 e.g. "ab". 중심이 2개 + String even = palindrome(s, i, i + 1); + + String longer = odd.length() > even.length() ? odd : even; + if(longer.length() > answer.length()) { + answer = longer; + } + } + return answer; + } + String palindrome(String s, int start, int end) { + while(start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)) { + start--; + end++; + } + return s.substring(start+1, end); + } +} From 4b12e7c1b3083127ee4ef985186f3719e8963113 Mon Sep 17 00:00:00 2001 From: hyerijung Date: Wed, 10 Jun 2026 18:16:15 +0900 Subject: [PATCH 3/3] construct-binary-tree-from-preorder-and-inorder-traversal solution --- .../hyeri0903.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/hyeri0903.java diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/hyeri0903.java b/construct-binary-tree-from-preorder-and-inorder-traversal/hyeri0903.java new file mode 100644 index 0000000000..873918c06f --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/hyeri0903.java @@ -0,0 +1,60 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public TreeNode buildTree(int[] preorder, int[] inorder) { + /** + 1.preorder, inorder array를 기반으로 binary tree return + 2.constraints + - 모두 unique values + - length min = 1, max = 3000 + 3.solutions + - Root node = preorder[0] + - inorder 에서 root node 위치 찾기 -> root node 위치 left/right node + - inorder 에서 구한 left/right node 수로 preorder array 에서 left/right 나눔 + + */ + + return buildTree(preorder, 0, preorder.length - 1, + inorder, 0, inorder.length - 1); + + } + private TreeNode buildTree(int[] preorder, int preStart, int preEnd, + int[] inorder, int inStart, int inEnd) { + if(preStart > preEnd || inStart > inEnd) return null; + + //preorder 첫번째 값 = root node value + int rootVal = preorder[preStart]; + TreeNode root = new TreeNode(rootVal); + + //inorder 에서 root 찾기 + int rootIndex = 0; + for(int i = 0; i