From 9f239bb043f3d5c11fbd8500469ad6cb9f74d406 Mon Sep 17 00:00:00 2001 From: YoonSeok Han Date: Fri, 20 Feb 2026 20:02:31 +0900 Subject: [PATCH 1/2] Solutions #229 & #253 & #266 & #281 --- .../ys-han00.cpp | 40 ++++++++++++++ longest-palindromic-substring/ys-han00.cpp | 26 +++++++++ rotate-image/ys-han00.cpp | 19 +++++++ subtree-of-another-tree/ys-han00.cpp | 55 +++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 construct-binary-tree-from-preorder-and-inorder-traversal/ys-han00.cpp create mode 100644 longest-palindromic-substring/ys-han00.cpp create mode 100644 rotate-image/ys-han00.cpp create mode 100644 subtree-of-another-tree/ys-han00.cpp diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/ys-han00.cpp b/construct-binary-tree-from-preorder-and-inorder-traversal/ys-han00.cpp new file mode 100644 index 0000000000..dc8868119c --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/ys-han00.cpp @@ -0,0 +1,40 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + unordered_map indices; + int pre_idx = 0; + + TreeNode* dfs(const vector& preorder, int start, int end) { + if (start > end) + return nullptr; + + int root_val = preorder[pre_idx++]; + TreeNode* root = new TreeNode(root_val); + + int mid = indices[root_val]; + + root->left = dfs(preorder, start, mid - 1); + root->right = dfs(preorder, mid + 1, end); + + return root; + } + + TreeNode* buildTree(vector& preorder, vector& inorder) { + for (int i = 0; i < inorder.size(); ++i) + indices[inorder[i]] = i; + + pre_idx = 0; + + return dfs(preorder, 0, inorder.size() - 1); + } +}; \ No newline at end of file diff --git a/longest-palindromic-substring/ys-han00.cpp b/longest-palindromic-substring/ys-han00.cpp new file mode 100644 index 0000000000..24c8930ad2 --- /dev/null +++ b/longest-palindromic-substring/ys-han00.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + string longestPalindrome(string s) { + int max_s = 0, max_e = 0; + int n = s.size(); + vector> dp(n, vector(n, false)); + + for(int i = n - 1; i >= 0; i--) { + for(int j = i; j < n; j++) { + if(i == j) + dp[i][j] = true; + else if(i + 1 == j) + dp[i][j] = (s[i] == s[j]); + else + dp[i][j] = (s[i] == s[j]) && dp[i + 1][j - 1]; + + if(dp[i][j] && max_e - max_s < j - i) { + max_s = i; + max_e = j; + } + } + } + + return s.substr(max_s, max_e - max_s + 1); + } +}; \ No newline at end of file diff --git a/rotate-image/ys-han00.cpp b/rotate-image/ys-han00.cpp new file mode 100644 index 0000000000..41a393fbf7 --- /dev/null +++ b/rotate-image/ys-han00.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + void rotate(vector>& matrix) { + int top = 0, bottom = matrix.size() - 1; + + while(top < bottom) { + int left = top, right = bottom; + for(int i = 0; i < bottom - top; i++) { + int topLeft = matrix[top][left + i]; + matrix[top][left + i] = matrix[bottom - i][left]; + matrix[bottom - i][left] = matrix[bottom][right - i]; + matrix[bottom][right - i] = matrix[top + i][right]; + matrix[top + i][right] = topLeft; + } + top++; + bottom--; + } + } +}; \ No newline at end of file diff --git a/subtree-of-another-tree/ys-han00.cpp b/subtree-of-another-tree/ys-han00.cpp new file mode 100644 index 0000000000..1f4c09fa7f --- /dev/null +++ b/subtree-of-another-tree/ys-han00.cpp @@ -0,0 +1,55 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + bool isSameTree(TreeNode* p, TreeNode* q) { + if (p == nullptr && q == nullptr) return true; + if (p == nullptr || q == nullptr) return false; + if (p->val != q->val) return false; + + return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); + } + + bool isSubtree(TreeNode* root, TreeNode* subRoot) { + if (root == nullptr) return false; + if (isSameTree(root, subRoot)) return true; + return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot); + } + + // bool ans = false; + // void check(TreeNode* root, TreeNode* subRoot) { + // if(root == nullptr || subRoot == nullptr || root->val != subRoot->val) { + // if(root != nullptr || subRoot != nullptr) + // ans = false; + // return; + // } + // check(root->left, subRoot->left); + // check(root->right, subRoot->right); + // } + + // void rec(TreeNode* root, TreeNode* subRoot) { + // if(root == nullptr || ans) + // return; + // if(root->val == subRoot->val) { + // ans = true; + // check(root, subRoot); + // if(ans) return; + // } + // rec(root->left, subRoot); + // rec(root->right, subRoot); + // } + + // bool isSubtree(TreeNode* root, TreeNode* subRoot) { + // rec(root, subRoot); + // return ans; + // } +}; From 7574a7fac81cbe692bee1c2f6e6288ca038df2f9 Mon Sep 17 00:00:00 2001 From: YoonSeok Han Date: Fri, 20 Feb 2026 20:06:56 +0900 Subject: [PATCH 2/2] add line --- .../ys-han00.cpp | 2 +- longest-palindromic-substring/ys-han00.cpp | 2 +- rotate-image/ys-han00.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/ys-han00.cpp b/construct-binary-tree-from-preorder-and-inorder-traversal/ys-han00.cpp index dc8868119c..0901bad65c 100644 --- a/construct-binary-tree-from-preorder-and-inorder-traversal/ys-han00.cpp +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/ys-han00.cpp @@ -37,4 +37,4 @@ class Solution { return dfs(preorder, 0, inorder.size() - 1); } -}; \ No newline at end of file +}; diff --git a/longest-palindromic-substring/ys-han00.cpp b/longest-palindromic-substring/ys-han00.cpp index 24c8930ad2..0ac72da2a4 100644 --- a/longest-palindromic-substring/ys-han00.cpp +++ b/longest-palindromic-substring/ys-han00.cpp @@ -23,4 +23,4 @@ class Solution { return s.substr(max_s, max_e - max_s + 1); } -}; \ No newline at end of file +}; diff --git a/rotate-image/ys-han00.cpp b/rotate-image/ys-han00.cpp index 41a393fbf7..82c1d0d591 100644 --- a/rotate-image/ys-han00.cpp +++ b/rotate-image/ys-han00.cpp @@ -16,4 +16,4 @@ class Solution { bottom--; } } -}; \ No newline at end of file +};