-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path7-2.cpp
More file actions
executable file
·50 lines (49 loc) · 1.01 KB
/
7-2.cpp
File metadata and controls
executable file
·50 lines (49 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include <string.h>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T,char a[],int &i)
{//先序建立二叉树
if(a[i]=='#') T=NULL;
else
{
T=new BiTNode;
T->data=a[i];
CreateBiTree(T->lchild,a,++i);
CreateBiTree(T->rchild,a,++i);
}
}
BiTree pre=NULL; //前驱指针
void JudgeBST(BiTree T,int &flag)
{//判断二叉树T是否是二叉排序树,flag初值为1
if(T!=NULL&&flag)
{
JudgeBST(T->lchild,flag); //中序遍历左子树
if(pre==NULL) pre=T; //中序遍历的第一个结点不必判断
else if(pre->data<T->data) pre=T; //前驱指针指向当前结点
else flag=0; //不是二叉排序树
JudgeBST(T->rchild,flag); //中序遍历右子树
}
}
int main()
{
char a[99];
//输入先序序列
cin>>a;
if(strcmp(a,"#")!=0){
int i=-1;
int flag=1;
BiTree T;
CreateBiTree(T,a,++i);
JudgeBST(T,flag);
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}