1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #include<bits/stdc++.h> using namespace std;
#define LOCAL
#define PB push_back #define PF push_front
typedef long long ll; typedef unsigned long long ull;
struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} };
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll exgcd(ll l,ll r,ll &x,ll &y) { if(r == 0) { x = 1; y = 0; return l; } else { ll d = exgcd(r, l%r, y, x); y -= l/r*x; return d; } }
ll MOD(ll a, ll m) { a %= m; if(a < 0)a += m; return a; }
ll inverse(ll a, ll m) { a = MOD(a, m); if(a <= 1)return a; return MOD((1 - inverse(m, a) * m) / a, m); }
ll kasumi(ll a, ll b, ll mod) { a %= mod; if(b < 0)a = inverse(a, mod), b = -b; ll ans = 1; while(b) { if(b & 1)ans = ans * a % mod; a = a * a % mod; b /= 2; } return ans % mod; }
string serialize(TreeNode* root) { if (!root) { return "X"; } auto left = "(" + serialize(root->left) + ")"; auto right = "(" + serialize(root->right) + ")"; return left + to_string(root->val) + right; }
inline TreeNode* parseSubtree(const string &data, int &ptr) { ++ptr; auto subtree = parse(data, ptr); ++ptr; return subtree; }
inline int parseInt(const string &data, int &ptr) { int x = 0, sgn = 1; if (!isdigit(data[ptr])) { sgn = -1; ++ptr; } while (isdigit(data[ptr])) { x = x * 10 + data[ptr++] - '0'; } return x * sgn; }
TreeNode* parse(const string &data, int &ptr) { if (data[ptr] == 'X') { ++ptr; return nullptr; } auto cur = new TreeNode(0); cur->left = parseSubtree(data, ptr); cur->val = parseInt(data, ptr); cur->right = parseSubtree(data, ptr); return cur; }
TreeNode* deserialize(string data) { int ptr = 0; return parse(data, ptr); }
#ifdef LOCAL
int main() { freopen("testdata.in", "r", stdin); } #endif
|