View Distributions.cpp
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<string> | |
#include<random> | |
#include<vector> | |
#include<sstream> | |
#include<chrono> | |
using namespace std; | |
int main(){ |
View RandomEngine.cpp
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<string> | |
#include<random> | |
#include<vector> | |
#include<sstream> | |
#include<chrono> | |
using namespace std; | |
void printRandom(default_random_engine e){ | |
for(int i = 0; i < 10; i++) |
View chrono.cpp
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
int main(){ | |
std::ratio<1, 10> r; //1/10 | |
cout << r.num << "/" << r.den <<endl; | |
//ALl clock frequency is represented by a ratio, we can print the | |
//ratio using the similar approach. | |
cout << chrono::system_clock::period::num <<"/" <<chrono::steady_clock::period::den <<endl; | |
//duration: | |
chrono::microseconds mi(2700);//2700 microseconds! | |
mi.count(); //get the value 2700 here |
View BT_LevelOrder_It.cpp
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: | |
vector<vector<int>> levelOrder(TreeNode* root) { | |
vector<vector<int>> res; | |
if(!root) return res; | |
//We can use the length of the tree to indicate the elements in this level | |
//A common trick, remember | |
queue<TreeNode*> Q; | |
Q.push(root); | |
while(!Q.empty()){ |
View BT_LevelOrder_Rec.cpp
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 { | |
private: | |
void dfs(vector<vector<int>>& res, TreeNode* node, int level){ | |
int len = res.size(); | |
//How to maintain the current layer list is critical here. | |
if(res.empty() || len < level + 1) | |
res.push_back(vector<int>()); | |
res[level].push_back(node->val); | |
if(node->left) dfs(res, node->left, level+1); | |
if(node->right) dfs(res, node->right, level + 1); |
View BT_TreeNode.cpp
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
struct TreeNode { | |
int val; | |
TreeNode *left; | |
TreeNode *right; | |
TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
}; |
View BT_Inorder_It02.cpp
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: | |
vector<int> inorderTraversal(TreeNode* root) { | |
vector<int> res; | |
if(!root) return res; | |
stack<TreeNode*> st; | |
TreeNode* pre = nullptr, *cur = root; | |
while(cur || !st.empty()){ | |
while(cur){ | |
st.push(cur); |
View BT_Inorder_It01.cpp
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: | |
vector<int> inorderTraversal(TreeNode* root) { | |
vector<int> res; | |
stack<TreeNode*> st; | |
if(root == nullptr) return res; | |
//Maintain a variable for current value. For inorder traversal, | |
//we need to push left nodes to our stack, then add the value to res. | |
TreeNode* cur = root; | |
while(cur || !st.empty()){ |
View BT_Inorder_Rec.cpp
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 { | |
private: | |
void dfs(vector<int>& res, TreeNode* root){ | |
if(root == nullptr) return; | |
dfs(res, root->left); | |
res.push_back(root->val); | |
dfs(res, root->right); | |
} | |
public: | |
vector<int> inorderTraversal(TreeNode* root) { |
View BT_Preorder_It02.cpp
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: | |
vector<int> preorderTraversal(TreeNode* root) { | |
vector<int> res; | |
if(!root) return res; | |
stack<TreeNode*> st; | |
TreeNode* pre = nullptr, *cur = root; | |
while(cur || !st.empty()){ | |
while(cur){ | |
st.push(cur); |
NewerOlder