#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
struct treenode{
struct treenode *lchild;
struct treenode *rchild;
struct treenode *father;
int level;
char value;
}*root;
int creattree2(treenode **root)
{
char data;
scanf("%c",&data);
if(data == '#')
{
(*root) = NULL;
return 0;
}
else
{
(*root) = (treenode *)malloc(sizeof(treenode));
(*root)->value = value;
creattree2(&(*root)->lchild);
creattree2(&(*root)->rchild);
}
return 0;
}
void preorder(treenode *T)
{
if(T != NULL)
{
printf("%c ",T->value);
preorder(T->lchild);
preorder(T->rchild);
}
}
void inorder(treenode *T)
{
if(T)
{
inorder(T->lchild);
printf("%c ",T->value);
inorder(T->rchild);
}
}
void postorder(treenode *T)
{
if(T)
{
postorder(T->lchild);
postorder(T->rchild);
printf("%c ",T->value);
}
}
void levelorder(treenode *T)
{
queue <treenode *> que;
que.push(T);
while(!que.empty())
{
treenode *tmp = que.front();
que.pop();
printf("%c ", tmp->value);
if(tmp->lchild != NULL)
que.push(tmp->lchild);
if(tmp->rchild != NULL)
que.push(tmp->rchild);
}
}
int tree_size(treenode *T)
{
if(T == NULL)
return 0;
else
return 1 + tree_size(T->lchild) + tree_size(T->rchild);
}
int high(treenode *T)
{
if(T == NULL)
return 0;
else
{
int left = high(T->lchild);
int right = high(T->rchild);
return left < right ? right + 1 : left + 1;
}
}
treenode *copy(treenode *T)
{
if(T == NULL)
return NULL;
treenode *tmp = new treenode;
tmp->value = T->value;
tmp->lchild = copy(T->lchild);
tmp->rchild = copy(T->rchild);
return tmp;
}
bool isequal(treenode *root1, treenode *root2)
{
if(root1 == NULL && root2 == NULL)
return true;
if(root1 != NULL && root2 != NULL && root1->value == root2->value
&& isequal(root1->lchild, root2->lchild)
&& isequal(root2->rchild, root2->rchild)
)
return true;
else
return false;
}
treenode *rebuildtree_1(char *pre, char *mid, int len)
{
if(!len)
return NULL;
int index = 0;
while(mid[index] != pre[0])
index++;
treenode *root = new treenode;
root->value = pre[0];
root->lchild = rebuildtree_1(pre, mid, index);
root->rchild = rebuildtree_1(pre+index+1, mid+index+1, len-index-1);
return root;
}
treenode *rebuildtree_2(char *mid, char *post, int len)
{
if(len == 0)
return NULL;
int index = 0;
while(mid[index] != post[len - 1])
index++;
treenode *root = new treenode;
root->value = post[len - 1];
root->lchild = rebuildtree_2(mid, post, index);
root->rchild = rebuildtree_2(mid+index+1, post+index, len-index-1);
return root;
}