#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
typedef struct tree
{
char data;
tree* left;
tree* right;
}tree;
tree* creat(string f, string i, int left1, int right1, int left2, int right2)
{
if (left1 > right1 || left2 > right2)
{
return nullptr;
}
tree* root = new tree;
root->data = f[left1];
int mid = 0;
while (i[mid] != root->data)
{
mid++;
}
int leftsize = mid - left2;
root->left = creat(f, i, left1 + 1, left1 + leftsize, left2, mid - 1);
root->right = creat(f, i, left1 + leftsize + 1, right1, mid + 1, right2);
return root;
}
tree* creat1(string i, string b, int left1, int right1, int left2, int right2)
{
if (left1 > right1 || left2 > right2)
{
return nullptr;
}
tree* root = new tree;
root->data = b[right2];
int mid = 0;
while (b[mid] != root->data)
{
mid++;
}
int leftsize = mid - left1;
root->left = creat1(i, b, left1, mid-1, left2, left2+leftsize);
root->right = creat1(i, b, mid+1, right1, left2+leftsize+1, right2-1);
}
tree* creat1(string f, string b, int left1, int right1, int left2, int right2)
{
if (left1 > right1 || left2 > right2)
{
return nullptr;
}
tree* root = new tree;
root->data = f[left1];
int mid = left2;
while (b[mid] != f[left1+1])
{
mid++;
}
int leftsize = mid - left1;
root->left = creat1(f, b, left1+1, left1+leftsize+1, left2, mid);
root->right = creat1(f, b, left1+leftsize+2, right1, mid+1, right2 - 1);
}
void pf(tree* root)
{
if (root == nullptr)
{
return;
}
cout << root->data;
pf(root->left);
pf(root->right);
}
void pi(tree* root)
{
if (root == nullptr)
{
return;
}
pi(root->left);
cout << root->data;
pi(root->right);
}
void pb(tree* root)
{
if (root == nullptr)
{
return;
}
pb(root->left);
pb(root->right);
cout << root->data;
}
void setf(tree* root, vector<char>& s)
{
stack<tree*>st;
tree* cur = root;
while (cur != nullptr ||!s.empty())
{
while (cur)
{
st.push(cur);
s.push_back(cur->data);
cur = cur->left;
}
tree* cur1 = st.top();
st.pop();
cur = cur1->right;
}
}
void seti(tree* root, vector<char>& s)
{
stack<tree*>st;
tree* cur = root;
while (cur != nullptr || !s.empty())
{
while (cur)
{
st.push(cur);
cur = cur->left;
}
tree* cur1 = st.top();
st.pop();
s.push_back(cur1->data);
cur = cur1->right;
}
}
void seti(tree* root, vector<char>& s)
{
stack<tree*>st;
tree* cur = root;
tree* prev = nullptr;
while (cur != nullptr || !s.empty())
{
while (cur)
{
st.push(cur);
cur = cur->left;
}
tree* cur1 = st.top();
if (cur1->right == nullptr || cur1->right == prev)
{
st.pop();
s.push_back(cur1->data);
prev = cur1;
}
else
{
cur = cur1->right;
}
}
}
void chengxu(tree* root, vector<vector<char>>&s)
{
if (root == nullptr)
{
return ;
}
queue<tree*>qu;
qu.push(root);
while (!qu.empty())
{
int n = qu.size();
s.push_back(vector<char>());
for (int i = 0; i < n; i++)
{
tree* point = qu.front();
qu.pop();
s.back().push_back(point->data);
if (point->left)
{
qu.push(point->left);
}
if (point->right)
{
qu.push(point->right);
}
}
}
}