程序员算法之判断序列是否是二叉查找树的后续

View Code
 1 #include <stdio.h>

 2 bool VerifySquenceBST(int *Squence,int length)

 3 {

 4     if(Squence == NULL || length <= 0) return false;

 5     int root = Squence[length - 1];

 6     for(int i = 0;i < length-1;++i)

 7     {

 8         if(Squence[i] > root) break;

 9     }

10     int j = i;

11     for(;j < length-1;++j)

12     {

13         if(Squence[j] < root) return false;

14     }

15 

16     bool left = true;

17     if(i > 0)

18     {

19         left = VerifySquenceBST(Squence,i);

20     }

21     bool rigth = true;

22     if(j < length-1)

23     {

24         rigth = VerifySquenceBST(Squence + i,length-i-1);

25     }

26     if(left && rigth) return true;

27     else return false;

28 }

29 

30 int main()

31 {

32     int Squence[7] = {5,7,6,9,11,10,8};    

33     bool test = VerifySquenceBST(Squence,7);

34     if(test == true) printf("The Squence is BST\n");

35     else printf("The Squence is not BST\n");

36     return 0;

37 }

 

你可能感兴趣的:(二叉查找树)