Welcome to Jekyll!
You’ll find this post in your _posts
directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run jekyll serve
, which launches a web server and auto-regenerates your site when a file is updated.
To add new posts, simply add a file in the _posts
directory that follows the convention YYYY-MM-DD-name-of-post.ext
and includes the necessary front matter. Take a look at the source for this post to get an idea about how it works.
Jekyll also offers powerful support for code snippets:
Check out the Jekyll docs for more info on how to get the most out of Jekyll. File all bugs/feature requests at Jekyll’s GitHub repo. If you have questions, you can ask them on Jekyll Talk.
#include <cassert>
#include <deque>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <utility>
#define endl '\n'
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode() : val(100), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
constexpr int null = -1;
//
// void connect(TreeNode* head, deque<int>& q) {
// if (q.empty()) return;
// int left = q.front();
// q.pop_front();
// int right = q.front();
// q.pop_front();
// if (left != null) {
// head->left = new TreeNode(left);
// }
// if (right != null) {
// head->right = new TreeNode(right);
// }
// if (left != null) {
// connect(head->left, q);
// }
// if (right != null) {
// connect(head->right, q);
// }
// }
void connect(TreeNode* head, deque<int>& q) {
int cnt = 0;
int left, right;
int mqcnt = 1;
queue<TreeNode*> mq;
TreeNode* cur;
mq.push(head);
while (!q.empty()) {
for (int i = 0; i < mqcnt; ++i) {
cur = mq.front();
mq.pop();
if (!q.empty()) {
left = q.front();
q.pop_front();
} else {
left = null;
}
if (!q.empty()) {
right = q.front();
q.pop_front();
} else {
right = null;
}
if (left != null) {
++cnt;
cur->left = new TreeNode(left);
mq.push(cur->left);
}
if (right != null) {
++cnt;
cur->right = new TreeNode(right);
mq.push(cur->right);
}
}
mqcnt = cnt;
cnt = 0;
}
}
TreeNode* create(deque<int>& q) {
if (q.empty()) return nullptr;
TreeNode* ret = new TreeNode(q.front());
q.pop_front();
connect(ret, q);
return ret;
}
vector<string> draw(int width, TreeNode* head) {
vector<string> ret;
if (!head) {
ret.push_back(string(width, '.'));
return ret;
}
vector<string> left;
vector<string> right;
string s = to_string(head->val);
ret.push_back(s + string(width - s.size(), '.'));
if (head->left) {
left = draw(width / 2, head->left);
if (head->right) { // both
right = draw(width - width / 2, head->right);
} else { // only left
right.push_back(string(width - width / 2, '.'));
}
} else {
if (head->right) { // only right
right = draw(width / 2, head->right);
left.push_back(string(width - width / 2, '.'));
} else { // no child
string s = string(width, '.');
ret.push_back(s);
return ret;
}
}
if (left.size() < right.size()) {
left.insert(left.end(), right.size() - left.size(), *(left.end() - 1));
// for (int i = 0; i < right.size() - left.size(); ++i) { //why this is
// not working?
// left.push_back(left[left.size() - 1]);
// }
} else if (left.size() > right.size()) {
right.insert(right.end(), left.size() - right.size(), *(right.end() - 1));
// for (int i = 0; i < left.size() - right.size(); ++i) {
// right.push_back(right[right.size() - 1]);
// }
}
assert(left.size() == right.size());
for (int i = 0; i < right.size(); ++i) {
ret.push_back(left[i] + right[i]);
}
return ret;
}
void callDraw(TreeNode* head, int width) {
vector<string> v = draw(width, head);
for (const auto& a : v) {
cout << a << endl;
}
}
void printNode(const TreeNode* t) {
if (!t) return;
cout << t->val << endl;
if (t->left) {
cout << "go left from " << t->val << endl;
printNode(t->left);
}
if (t->right) {
cout << "go right from " << t->val << endl;
printNode(t->right);
}
}
/*
class OldSolution {
public:
int ret = 0;
enum { LEFT, RIGHT };
int count(TreeNode* root, int dir) {
if (!root)
return 0;
if (dir == LEFT) {
int r = max(count(root->right, LEFT), 1 + count(root->left,
RIGHT)); ret = max(ret, r); return r; } else { int r =
max(count(root->left, RIGHT), 1 + count(root->right, LEFT)); ret =
max(ret, r); return r;
}
}
int longestZigZag(TreeNode* root) {
int r = 1 + max(count(root->right, LEFT), count(root->left, RIGHT));
ret = max(ret, r);
return ret - 1;
}
};
class Solution {
public:
int mx = 0;
void dfs(TreeNode* root, bool left, int count) {
if (!root)
return 0;
mx = max(mx, count);
if (left) {
dfs(root->left, false, count + 1);
dfs(root->right, true, 1);
} else {
dfs(root->left, false, 1);
dfs(root->right, true, count + 1);
}
}
int longestZigZag(TreeNode* root) {
dfs(root, true, 0);
dfs(root, false, 0);
return mx;
}
};
*/
int main() {
// vector<int> v = {1, null, 1, 1, 1, null, null, 1,
// 1, null, 1, null, null, null, 1};
//
vector<int> v = {1, 2, 3, null, 4, null, null, 5, 6, null, 7};
// vector<int> v = {1, null, 1, null, 1, 1, 1, null, null, null, null};
// vector<int> v = {1, null, 1, 1, 1, null, null, null, null};
// vector<int> v = {1, null, null};
deque<int> q;
for (auto i : v)
q.push_back(i);
TreeNode* root = create(q);
printNode(root);
cout << endl;
callDraw(root, 100);
}