#include"vector"
#include"cstring"
#include"algorithm"
using namespace std
const int maxn =30
struct node{
int data
node* lchild
node* rchild
}
int n
int in[maxn]
bool vis[maxn]={false}
vector<int>lev
node* create(vector<int>lev,int inl,int inr){
if(lev.size()==0) return NULL
if(inl>inr) return NULL
//printf("00\n")
node* root= new node
root->data =lev[0]
int k
for(k=inlk<=inrk++){
if(lev[0]==in[k])
break
}
for(int j=inlj<=k-1j++)
vis[in[j]]=true
vector<int>tempLeft,tempRight//要函数体内新建
for(int i=1i<lev.size()i++){
if(vis[lev[i]]==true)
tempLeft.push_back(lev[i])
else
tempRight.push_back(lev[i])
}
root->lchild =create(tempLeft,inl,k-1)
root->rchild =create(tempRight,k+1,inr)
return root
}
void preorder(node* root){
if(root==NULL)
return
printf("%d ",root->data)
preorder(root->lchild)
preorder(root->rchild)
}
int main(){
scanf("%d",&n)
int x
for(int i=0i<ni++){
scanf("%d",&x)
lev.push_back(x)
}
for(int j=0j<nj++)
scanf("%d",&in[j])
node *root =create(lev,0,n-1)
preorder(root)
return 0
}
#include<iostream.h>
#include
<stdio.h>
#include
<malloc.h>
#define
MaxNode
100
typedef
char
DataType
typedef
struct
node
{
DataType
data
struct
node
*lchild
struct
node
*rchild
}BiTNode,BiTree
void
CreateBiTree(BiTree
*bt)//建立一个二叉树
{
char
ch
//ch=getchar()
scanf("%c",&ch)
if
(ch=='
')
bt=NULL
else{
bt=(BiTree*)malloc(sizeof(BiTNode))
bt->data=ch
CreateBiTree(bt->lchild)
CreateBiTree(bt->rchild)
}
}
void
PreOrder(BiTree
*root)//前序遍历
{
if(root!=NULL)
{
Visit(root->data)
PreOrder(root->lchild)
PreOrder(root->rchild)
}
}
void
InOrder(BiTree
*root)//中序遍历
{
if(root!=NULL)
{
InOrder(root->lchild)
Visit(root->data)
InOrder(root->rchild)
}
}
void
LaOrder(BiTree
*root)//后序遍历
{
if(root!=NULL)
{
PreOrder(root->lchild)
PreOrder(root->rchild)
Visit(root->data)
}
}
void
main()
{
BiTree
*bt
printf("请输入数据:\n")
bt=
NULL
CreateBiTree(bt)
//and
here
printf("\n
结果如下:\n")
printf("先序遍历的结果为:\n")
PreOrder(bt)
printf("\n")
printf("中序遍历的结果为:\n")
InOrder(bt)
printf("\n")
printf("后序遍历的结果为:\n")
LaOrder(bt)
}
有个Visit()函数
你没写!!
我只是改了语法错误!!
只剩那一个函数没定义
你定义下就没语法错误了!
!
#include"stdio.h"//二叉树#include"stdlib.h"
typedef
struct
node
{
char
data
struct
node
*lchild,*rchild
}BinTNode
typedef
BinTNode*
BinTree
void
GreateBinTree(BinTree
*T)//以先序遍历为依据构造二叉树,T为指向根指针的指针.
{
//空结点以空格代替.
char
ch
if((ch=getchar())=='
')
*T=NULL
else
{
*T=(BinTree)malloc(sizeof(BinTNode))
(*T)->data=ch
GreateBinTree(&((*T)->lchild))
GreateBinTree(&((*T)->rchild))
}
}
void
Lorder(BinTree
T)//先序遍历二叉树.
{
if(T)
{
printf("%c
",T->data)
Lorder(T->lchild)
Lorder(T->rchild)
}
}
void
Morder(BinTree
T)//中序遍历二叉树.
{
if(T)
{
Morder(T->lchild)
printf("%c
",T->data)
Morder(T->rchild)
}
}
void
Rorder(BinTree
T)//后序遍历二叉树.
{
if(T)
{
Rorder(T->lchild)
Rorder(T->rchild)
printf("%c
",T->data)
}
}