stack<TreeNode*> stk;
BSTIterator(TreeNode* root) {
while(root){
stk.push(root);
root = root->left;
}
}
int next() {
auto p = stk.top();
stk.pop();
int val = p->val;
p = p->right;
while(p){
stk.push(p);
p=p->left;
}
return val;
}
bool hasNext() {
return !stk.empty();
}
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
string res;
dfs1(root,res);
return res;
}
void dfs1(TreeNode* root,string &res){
if(!root){
res += "#,";
return ;
}
res += to_string(root->val) + ",";
dfs1(root->left,res);
dfs1(root->right,res);
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
int u = 0;
return dfs2(data,u);
}
TreeNode* dfs2(string &data,int &u){
if(data[u] == '#'){
u += 2;
return NULL;
}
int t = 0;
bool is_minus = false;
if(data[u] == '-'){
is_minus = true;
u ++;
}
while(data[u] != ','){
t = t * 10 + data[u] - '0';
u ++;
}
u ++;
if(is_minus) t = -t;
auto root = new TreeNode(t);
root->left = dfs2(data,u);
root->right = dfs2(data,u);
return root;
}