# ConvertBinarySearchTree

#define Nil 0
#include<stdio.h>
#include<stdlib.h>
typedef struct BiTNode{
int data;
BiTNode* lchild;
BiTNode* rchild;
}*BiTree;
void CreateBiTree(BiTree &T){
int temp;
printf("请输入节点的值\n");
scanf("%d",&temp);
if(temp==Nil)
T=NULL;
else{
T=(BiTNode*)malloc(sizeof(BiTNode));
if(!T){
printf("内存分配不足\n");
exit(0);
}
T->data=temp;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void ConvertBinarySearchTree(BiTree pNode,BiTree &lastNode){
if(pNode==NULL)
return ;
BiTNode* currNode=pNode;
if(pNode->lchild!=NULL)
ConvertBinarySearchTree(pNode->lchild,lastNode);
currNode->lchild=lastNode;
if(lastNode!=NULL)
lastNode->rchild=currNode;
lastNode=currNode;
if(pNode->rchild!=NULL)
ConvertBinarySearchTree(pNode->rchild,lastNode);
}
BiTNode* Convert(BiTree T){
BiTNode* lastNode=NULL;
BiTNode* pNode=T;
ConvertBinarySearchTree(pNode,lastNode);
while(lastNode!=NULL&&lastNode->lchild!=NULL)
lastNode=lastNode->lchild;
return lastNode;
}
void PrintList(BiTree T){
BiTNode* p=T;
BiTNode* q=NULL;
while(p){
printf("%3d",p->data);
q=p;
p=p->rchild;
}
printf("\n");
while(q){
printf("%3d",q->data);
q=q->lchild;
}
}
void main(){
BiTree T;
CreateBiTree(T);