# 数据结构之二叉树常见面试题

treenode *_treecreat(treenodetype data[],size_t size,size_t *index,treenodetype null_node)
{
if(index == NULL)
{
return NULL;//非法输入
}
if((*index) >= size)
{
return NULL;//遍历完了
}
if(data[*index] == null_node)
{
return NULL;//返回空树
}
treenode *newnode = treenode_creatnode(data[*index]);
++(*index);
newnode->lchild = _treecreat(data,size,index,null_node);
++(*index);
newnode->rchild = _treecreat(data,size,index,null_node);
return newnode;

}
treenode *treecreat(treenodetype data[],size_t size,treenodetype null_node)
{
size_t index = 0;
treenode *newnode = _treecreat(data,size,&index,null_node);
return newnode;
}


treenode *treenode_clone(treenode *root)
{
if(root == NULL)
{
return NULL;
}
treenode *newnode = treenode_creatnode(root->data);
newnode->lchild = treenode_clone(root->lchild);
newnode->rchild = treenode_clone(root->rchild);
return newnode;

}


size_t treeleafsize(treenode *root)
{
if(root == NULL)
{
return 0;
}
if(root->lchild == NULL && root->rchild == NULL)
{
return 1;
}
return treeleafsize(root->lchild) + treeleafsize(root->rchild);
}


size_t treeKlevelsize(treenode *root,int k)
{
if(root == NULL)
{
return 0;
}
if(k == 1)
{
return 1;
}
return treeKlevelsize(root->lchild,k-1) + treeKlevelsize(root->rchild,k-1);
}


size_t treehight(treenode *root)
{
if(root == NULL)
{
return 0;
}
size_t rhight = treehight(root->rchild);
size_t lhight = treehight(root->lchild);
return 1+(rhight > lhight ? rhight : lhight);
}


treenode *treefind(treenode *root,treenodetype value)
{
if(root == NULL)
{
return;
}
seqqueue queue;
seqqueuetype front;
seqqueue_init(&queue);
seqqueue_push(&queue,front);
while(1)
{
seqqueue_gettop(&queue,&front);
if(front->data = value)
{
return front;
}
if(front->lchild != NULL)
{
seqqueue_push(&queue,front->lchild);
}
if(front->rchild != NULL)
{
seqqueue_push(&queue,front->rchild);
}
seqqueue_pop(&queue);
}
}


void PreOrderByLoop(treenode *root)
{
if(root == NULL)
{
return;
}
seqstack s;
treenode *top;
treenode *cur = root;
seqstack_init(&s);
while(s.size != 0 || cur != NULL)
{
while(cur != NULL)
{
printf("%c ",cur->data);
seqstack_pushback(&s,cur);
cur = cur->lchild;
}
seqstack_gettop(&s,&top);
seqstack_popback(&s);
cur = top->rchild;
}
printf("\n");
}

void InOrderByLoop(treenode *root)
{
if(root == NULL)
{
return;
}
seqstack s;
treenode *cur = root;
treenode *top;
seqstack_init(&s);
while(s.size != 0 || cur != NULL)
{
while(cur != NULL)
{
seqstack_pushback(&s,cur);
cur = cur->lchild;
}
seqstack_gettop(&s,&top);
printf("%c ",top->data);
seqstack_popback(&s);
cur = top->rchild;
}
}


void PostOrderByLoop(treenode *root)
{
if(root == NULL)
{
return;
}
seqstack s;
treenode *cur = root;
treenode *top;
treenode *prev = NULL;
seqstack_init(&s);
while(s.size != 0 || cur != NULL)
{
while(cur != NULL)
{
seqstack_pushback(&s,cur);
cur = cur->lchild;
}
seqstack_gettop(&s,&top);
if(top->rchild == NULL || top->rchild == prev)
{
printf("%c ",top->data);
prev = top;
seqstack_popback(&s);
}
else
{
cur = top->rchild;
}
}
}


void swap(treenode **a,treenode **b)
{
treenode *t = *a;
*a = *b;
*b = t;
}
void TreeMirror(treenode *root)
{
if(root == NULL)
{
return;
}
swap(&root->lchild,&root->rchild);
if(root->lchild != NULL)
{
TreeMirror(root->lchild);
}
if(root->rchild != NULL)
{
TreeMirror(root->rchild);
}
}