Skip to content

Instantly share code, notes, and snippets.

View zhangxiaomu01's full-sized avatar
🎯
Focusing

Jun Zhang zhangxiaomu01

🎯
Focusing
View GitHub Profile
#include<iostream>
#include<string>
#include<random>
#include<vector>
#include<sstream>
#include<chrono>
using namespace std;
int main(){
#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++)
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
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()){
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);
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
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);
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()){
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) {
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);