-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarytreeornot.c
74 lines (53 loc) · 1017 Bytes
/
binarytreeornot.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *rnext;
struct node *lnext;
}*n,*prev,*extra;
struct node *create(struct node *n){
n=(struct node *)malloc(sizeof(struct node));
scanf("%d",&n->data);
n->rnext=NULL;
n->lnext=NULL;
return n;
}
void preorder(struct node*n){
if(n==NULL)
return;
printf("%d ",n->data);
preorder(n->lnext);
preorder(n->rnext);
}
int isbst(struct node*n){
static struct node *prev=NULL;
if(n==NULL)
return 1;
if(!isbst(n->lnext))
return 0;
if(prev!=NULL&&n->data<=prev->data){
return 0;
}
prev=n;
return isbst(n->rnext);
}
void postorder(struct node*n){
if(n==NULL)
return;
postorder(n->lnext);
postorder(n->rnext);
printf("%d ",n->data);
}
int main(){
struct node *root=create(n);
struct node *p1=create(n);
struct node *p2=create(n);
root->lnext=p1;
root->rnext=p2;
struct node *p3=create(n);
struct node *p4=create(n);
p1->lnext=p3;
p1->rnext=p4;
n=root;
printf("%d",isbst(n));
}