This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
// function template examples | |
template <typename T> | |
void print_vector(const vector<T>& temp){ | |
cout<<"{"; | |
for(int i=0;i<temp.size();i++){ | |
cout<<temp[i]; | |
if(i!= temp.size()-1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
/* | |
Time complexity: | |
getMin() : O(1) | |
extractMin(): O(LogN) | |
decreaseKey : O(LogN) | |
insert : O(LogN) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include<bits/stdc++.h> | |
using namespace std; | |
typedef pair<int,int> pi; | |
int main() { | |
int t; | |
cin>>t; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool isSameTree(TreeNode* p, TreeNode* q) { | |
if(p== nullptr and q == nullptr) // reached the end, so far it passed, so return true | |
return true; | |
if(p== nullptr || q== nullptr) // failed,before traversing the entire tree | |
return false; | |
if(p->val != q->val) // again a failure | |
return false; | |
return isSameTree(p->left,q->left) && isSameTree(p->right,q->right); //both of the left and right child must pass the test ! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int sum =0; | |
TreeNode* convertBST(TreeNode* root) { | |
if(root == nullptr) | |
return nullptr; | |
convertBST(root->right); | |
sum += root->val; | |
root->val = sum ; | |
convertBST(root->left); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string tree2str(TreeNode* root){ | |
if(root == nullptr) | |
return ""; | |
else | |
return root->val + "(" + tree2str(root->left) + ")" + "(" + tree2str(root->right) + ")"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def almostIncreasingSequence(sequence) | |
max = -100000 | |
mis_match = 0 | |
second_max = -10000 | |
sequence.each do |val| | |
if val > max | |
second_max = max | |
max = val | |
elsif val > second_max | |
max = val |
NewerOlder