关于二叉树的问题(怎么根据先序和中序遍历的结果建立二叉树?)

来源:百度知道 编辑:UC知道 时间:2024/05/23 18:46:06
(1)根据给定二叉树的先序遍历和中序遍历结果,构造出该二叉树;
(2)给出该二叉树的后序遍历结果;
(3)判定该二叉树是否为平衡二叉树;

#include<stdio.h>
#include<stdlib.h>

typedef char TElemType;

//Status是函数的类型,其值是函数结果状态码
typedef int status;

//函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

int w=0;

#define Link 0
#define Thread 1

typedef struct BiThrNode
{
TElemType data;
struct BiThrNode *lchild,*rchild; //左右孩子指针
int LTag,RTag;
}BiThrNode,*BiThrTree;

//构造二叉链表表示的二叉树
status createbitree(BiThrTree &T)
{
char ch;
scanf("%c",&ch);
if(ch=='$') T=NULL;
else
{
if(!(T=(BiThrNode *)malloc(sizeof(BiThrNode))))exit(OVERFLOW);
T->data =ch;
T->LTag=Link;
T->RTag=Link;
createbitree(T->lchild);
createbitree(T->rchild);
}
return OK;
}

void InThreading(BiThrTree &p,Bi