Skip to content

Commit 2af3fba

Browse files
Added solution
1 parent c89bba2 commit 2af3fba

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

longest_palindromic_subsequence.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int longestPalindromeSubseq(string s) {
4+
5+
if(s == "") return 0;
6+
vector<vector<int>> dp(s.size(), vector<int>(s.size(), 0));
7+
8+
for(int i=0; i<s.size(); i++){
9+
dp[i][i] = 1;
10+
}
11+
12+
for(int i=s.size() - 1; i>=0; i--){
13+
for(int j=i+1; j<s.size(); j++){
14+
// cout<<"i : j => "<<i<<j<<"\t";
15+
16+
if(s[i] == s[j])
17+
dp[i][j] = dp[i+1][j-1] + 2;
18+
else
19+
dp[i][j] = max(dp[i+1][j], dp[i][j-1]);
20+
}
21+
//cout<<"\n";
22+
23+
}
24+
return dp[0][s.size() - 1];
25+
}
26+
};

search_in_bst.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
13+
TreeNode* helper(TreeNode* root, int val){
14+
if(root == NULL) return root;
15+
16+
if(root->val == val) return root;
17+
else if(root->val < val) return helper(root->right, val);
18+
else return helper(root->left,val);
19+
20+
return NULL;
21+
}
22+
23+
TreeNode* searchBST(TreeNode* root, int val) {
24+
25+
if(root == NULL) return root;
26+
return helper(root, val);
27+
}
28+
};

0 commit comments

Comments
 (0)