Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<int, int> indices;
int pre_idx = 0;

TreeNode* dfs(const vector<int>& 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<int>& preorder, vector<int>& inorder) {
for (int i = 0; i < inorder.size(); ++i)
indices[inorder[i]] = i;

pre_idx = 0;

return dfs(preorder, 0, inorder.size() - 1);
}
};
26 changes: 26 additions & 0 deletions longest-palindromic-substring/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
string longestPalindrome(string s) {
int max_s = 0, max_e = 0;
int n = s.size();
vector<vector<bool>> dp(n, vector<bool>(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);
}
};
19 changes: 19 additions & 0 deletions rotate-image/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
void rotate(vector<vector<int>>& 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--;
}
}
};
55 changes: 55 additions & 0 deletions subtree-of-another-tree/ys-han00.cpp
Original file line number Diff line number Diff line change
@@ -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;
// }
};