《剑指Offer》读书笔记---面试题24:二叉搜索树的后序遍历序列

来源:互联网 发布:java积分商城系统源码 编辑:程序博客网 时间:2024/06/01 08:52

原文链接 http://blog.csdn.net/gzzheyi/article/details/8790267


题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则返回true,否则返回false。假设输入的数组的任意两个数字都互不相同。

例如输入数组{5,7,6,9,11,10,8},则返回true,如果输入的数组是{7,4,6,5},则返回false。


我的思路:

由于之前看了那个重组二叉树的题目,所以很容易就想出类似的解法。

一个二叉搜索树的后序遍历结果中,序列的最后一个元素肯定是这棵树的根结点,而紧跟在前的是根结点的右子树。那么这些右子树的结点都会比根结点大,而根结点点左子树的值都比根结点小。这样的话,我就可以从根结点开始向后遍历,直到找到一个比根结点值要小的结点,这个结点就是左子树的最后一个结点。然后继续遍历,如果在遍历过程中没有找到比根结点大的结点的话,则证明符合后序遍历规则。如果继续遍历的过程中再次发现比根结点大的结点,则证明不符合后序遍历规则。(代码中显示了边界情况,则只有右子树或者左子树的情况)。

然后分别递归判断上面找到的左子树和右子树序列。


详见代码:

[cpp] view plaincopy
  1. #include<cstdio>  
  2. #include<iostream>  
  3.   
  4. const int N = 100 ;  
  5.   
  6. bool IsPostOrder(int *PostOrder ,int nLength) ;   
  7. bool IsPostOrderCore(int *PostOrder ,int nLength) ;  
  8.   
  9. int main(void)  
  10. {  
  11.     int n ;  
  12.     int PostOrderSeq[N] ;  
  13.   
  14.     freopen("in.txt","r",stdin) ;  
  15.   
  16.     while(scanf("%d",&n) != EOF)  
  17.     {  
  18.         int i ;  
  19.         for(i = 0 ; i < n ; ++i)  
  20.         {  
  21.             scanf("%d",&PostOrderSeq[i]) ;  
  22.         }  
  23.           
  24.         bool IsTrue = IsPostOrder(PostOrderSeq,n) ;  
  25.   
  26.         if(true == IsTrue)  
  27.         {  
  28.             printf("Yes\n") ;  
  29.         }  
  30.         else  
  31.         {  
  32.             printf("No\n") ;  
  33.         }  
  34.     }  
  35.   
  36.     return 0 ;  
  37. }  
  38.   
  39.   
  40. bool IsPostOrder(int *PostOrder ,int nLength)   
  41. {  
  42.     if(NULL == PostOrder || 0 == nLength)  
  43.     {  
  44.         return false ;  
  45.     }  
  46.   
  47.     return IsPostOrderCore(PostOrder,nLength) ;  
  48. }  
  49.   
  50. bool IsPostOrderCore(int *PostOrder,int nLength)  
  51. {  
  52.     if(NULL == PostOrder) //无效输入   
  53.     {  
  54.         return false ;  
  55.     }  
  56.     if(nLength <= 1) //当剩下一个元素的时候,证明后序遍历过程正确,递归过程结束  
  57.     {  
  58.         return true ;  
  59.     }  
  60.   
  61.     int nRootValue = PostOrder[nLength-1] ;     //根结点的根  
  62.     int nRightLength = nLength ;                //根结点右子树的值,默认根只有右子树,是一种边界情况  
  63.     int *pPostEnd = PostOrder + nLength - 1 ;   //后序遍历序列的末尾  
  64.     int *pPostBeg = PostOrder ;                 //后序遍历序列的开始   
  65.     int *pRightBeg = PostOrder ;                //后序遍历中,根结点右子树的开始结点,默认根只有右子树,是一种边界情况   
  66.   
  67.     pPostEnd-- ;  
  68.     while(pPostEnd >= pPostBeg)                  //从序列最后开始查找,找到左右子树的分界点  
  69.     {  
  70.         if(*pPostEnd < nRootValue && nLength == nRightLength)  //找到分界点  
  71.         {  
  72.             nRightLength = pPostBeg + nLength - pPostEnd - 1 ;  
  73.             pRightBeg = pPostEnd + 1 ;  
  74.               
  75.         }  
  76.         else if(*pPostEnd > nRootValue && nRightLength != nLength)  //出错情况,也就是不符合后序遍历的情况  
  77.         {  
  78.             return false ;  
  79.         }  
  80.         pPostEnd-- ;  
  81.     }  
  82.   
  83.     int nLeftLength = nLength - nRightLength ; //左子树的结点数  
  84.       
  85.     return IsPostOrderCore(pPostBeg,nLeftLength) && IsPostOrderCore(pRightBeg,nRightLength-1) ; //递归地判断左右子树是否也是符合后序遍历  
  86. }  


而书上的解法和我的基本一致:

在后序遍历得到的序列中,最后一个数字是树的根结点的值。数组中前面的数字可以分为两部分:第一部分是左子树结点的值,它们都比根结点的值小;第二部分是右子树的值,它们都比根结点的值大。

代码:

[cpp] view plaincopy
  1. bool VerifySequenceOfBST(int sequence[], int length)   
  2. {  
  3.     if(sequence ==  NULL || length <= 0)  
  4.     {  
  5.         return false ;  
  6.     }  
  7.   
  8.     int root = sequence[length-1] ;  
  9.   
  10.     //在二叉搜索树中的左子树的结点小于根结点  
  11.     int i = 0 ;  
  12.     for(; i < length - 1 ; ++i)  
  13.     {  
  14.         if(sequence[i] > root)  
  15.         {  
  16.             break ;  
  17.         }  
  18.     }  
  19.   
  20.     //二叉搜索树中右子树的结点大于根结点  
  21.     int j = i ;           
  22.     for(; j < length - 1 ; ++j)  
  23.     {  
  24.         if(sequence[j] < root)  
  25.         {  
  26.             return false ;  
  27.         }  
  28.     }  
  29.   
  30.     //判断左子树是不是二叉搜索树  
  31.     bool left = true ;  
  32.     if(i > 0)  
  33.     {  
  34.         left = VerifySequenceOfBST(sequence,i) ;  
  35.     }  
  36.   
  37.     //判断右子树是不是二叉搜索树  
  38.     bool right = true ;  
  39.     if(i < length - 1)  
  40.     {  
  41.         right = VerifySequenceOfBST(sequence + i, length - i - 1) ;  
  42.     }     
  43.   
  44.     return (left && right) ;  
  45. }  

测试数据:

[cpp] view plaincopy
  1. 7  
  2. 7 8 9 17 16 10 18   
  3. 4  
  4. 10 9 8 7  
  5. 7  
  6. 5 7 6 9 11 10 8  
  7. 4  
  8. 7 4 6 5  
  9. 5  
  10. 8 6 12 11 10  
  11. 4  
  12. 7 8 9 10  
  13. 1  
  14. 10  
  15. 2  
  16. 9 10  
  17. 2  
  18. 10 9  
  19. 10  
  20. 7 6 8 4 15 14 16 12 20 10  
0 0
原创粉丝点击