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..0901bad65c --- /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); + } +}; diff --git a/longest-palindromic-substring/ys-han00.cpp b/longest-palindromic-substring/ys-han00.cpp new file mode 100644 index 0000000000..0ac72da2a4 --- /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); + } +}; diff --git a/rotate-image/ys-han00.cpp b/rotate-image/ys-han00.cpp new file mode 100644 index 0000000000..82c1d0d591 --- /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--; + } + } +}; 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; + // } +};