#include <iostream>
using namespace std;
typedef struct Treenode {
char data;
struct Treenode *lchild;
struct Treenode *rchild;
}Treenode,*Tree;
//先序建树
void Creatree(Tree &T, char s[], int &i) {
if (s[i] == '#')
T = NULL;
else
{
T = new Treenode;
T->data = s[i];
Creatree(T->lchild,s,++i);
Creatree(T->rchild,s,++i);
}
}
//判断二叉排序树
int Judge(Tree &T) {
if (T == NULL)
return 1;
if (T->lchild && T->lchild->data > T->data)
return 0;
if (T->rchild && T->rchild->data < T->data)
return 0;
return Judge(T->lchild)&&Judge(T->rchild);
}
int main() {
char s[50];
while (cin >> s &&s[0]!='#' )
{
Tree T;
int i = -1;
Creatree(T, s, ++i);
if (Judge(T) == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
二叉排序树的判定
最新推荐文章于 2024-11-06 11:05:01 发布