二叉排序树后序遍历序列的判断


判断一个数字序列是BST二叉搜索树的后序遍历的结果

 1 /*******************BST后续遍历******************/
 2 /*
 3 *判断一个数字序列是BST二叉搜索树的后序遍历的结果
 4 */
 5 int isbackorder(int a[],int start,int root)
 6 {
 7     if(root <= start)
 8         return 0;
 9     int end = root-1;
10     int is_right = 1;
11     int leftroot = 0;
12 
13     while(start <= end){
14         if(is_right){
15             if(a[end] > a[root]){
16                 end--;
17             }
18             else{
19                 is_right = 0;
20                 leftroot = end;
21             }
22         }
23         else{
24             if(a[end] < a[root])
25                 end--;
26             else
27                 return -1;
28         }
29     }
30     return isbackorder(a,start,leftroot);
31     return isbackorder(a,leftroot+1,root-1);
32 }
33 void test_backorder()
34 {
35     int a[] = {1,5,4,7,9,8,5};
36     if(isbackorder(a,0,6))
37         cout<<"not back order"<<endl;
38     else
39         cout<<"back order true"<<endl;
40 }
原文地址:https://www.cnblogs.com/xiaoerhei/p/3679221.html