//重建二叉树:输入某二叉树的前序和中序遍历,重建出该二叉树
#include<stdio.h>
#include<malloc.h>
typedef struct binarytreenode
{
int value;
struct binarytreenode *left;
struct binarytreenode *right;
}binary;
binary* constructcore(int *startpreorder,int *endpreorder,int *startinorder,int *endinorder)
{
int *rootinorder=NULL;
int leftlength =0; //左子树的长度
int * leftpreend =NULL; //前序遍历中左子树的终结点
int rootvalue=startpreorder[0]; //前序遍历中的第一个结点的值便是根节点的值
binary *root = malloc(sizeof(binary));
root->value=rootvalue;
root->left=root->right=NULL;
if(startpreorder==endpreorder)
{
if(startinorder==endinorder&& *startpreorder==*startinorder) //前序遍历和中序遍历中都只有根节点的情况
return root;
else
printf("invalid input\n");
// return root;
}
//在中序遍历中找到根节点的值
rootinorder = startinorder;
while(rootinorder
根据二叉树的前序遍历和中序遍历构建二叉树的c语言完整代码
最新推荐文章于 2022-10-19 16:54:41 发布