zoukankan      html  css  js  c++  java
  • 二叉搜索树的后序遍历序列

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

    思路:需要遍历树,二叉排序树的特点是 lchild.key < root.key < rchild.key

    那么我们使用分治思想,先利用上面特点将左右子树分开,遇到第一个大于root.key的之后(右边)就是右子树,那么当我们在遍历右子树的时候如果遇到小于root.key的情况,那么就是错误的序列。

      1 #include "stdafx.h"
      2 #include<stdio.h>
      3 
      4 
      5 // BST:Binary Search Tree,二叉搜索树
      6 bool VerifySquenceOfBST(int sequence[], int length)
      7 {
      8     if(sequence == NULL || length <= 0)
      9         return false;
     10         
     11     int root = sequence[length - 1];
     12     
     13     // 在二叉搜索树中左子树的结点小于根结点
     14     int i = 0;
     15     for(; i < length - 1; ++i)
     16     {
     17         if(sequence[i] > root)
     18             break;
     19     }
     20     
     21     // 在二叉搜索树中右子树的结点大于根结点
     22     int j = i ;
     23     for(; j < length - 1; ++j)
     24     {
     25         if(sequence[j] < root)
     26             return false;
     27     }
     28     
     29     // 判断左子树是不是二叉搜索树
     30     bool left = true;
     31     if(i > 0)
     32         left = VerifySquenceOfBST(sequence, i);
     33     
     34     // 判断右子树是不是二叉搜索树
     35     bool right = true;
     36     if(i < length - 1)
     37         right = VerifySquenceOfBST(sequence + i , length - i - 1) ;
     38         
     39     return (left && right);
     40 }
     41 
     42 
     43 int main()
     44 {
     45     
     46 //test1    
     47 //            10
     48 //         /      
     49 //        6        14
     50 //       /        /
     51 //      4  8     12  16
     52     int data1[] = {4, 8, 6, 12, 16, 14, 10};
     53     int length1 = sizeof(data1) / sizeof(int);
     54     printf("test1:  ");
     55     if(VerifySquenceOfBST(data1, length1))
     56         printf("Yes
    ");
     57     else
     58         printf("No
    ");
     59     
     60 //test2    
     61 //               5
     62 //              /
     63 //             4
     64 //            /
     65 //           3
     66 //          /
     67 //         2
     68 //        /
     69 //       1
     70     
     71     int data2[] = {5, 4, 3, 2, 1};
     72     int length2 = sizeof(data2)/sizeof(int);
     73     printf("test2:  ");
     74     if(VerifySquenceOfBST(data2, length2))
     75         printf("Yes
    ");
     76     else
     77         printf("No
    ");
     78 
     79 //test3    
     80 //           5
     81 //          / 
     82 //         4   7
     83 //            /
     84 //           6
     85 
     86     int data3[] = {4, 6, 7, 5};
     87     int length3 = sizeof(data3)/sizeof(int);
     88     printf("test3:  ");
     89     if(VerifySquenceOfBST(data3, length3))
     90         printf("Yes
    ");
     91     else
     92         printf("No
    ");
     93     
     94 //test4
     95 // NULL
     96 
     97     printf("test4:  ");
     98     if(VerifySquenceOfBST(NULL, 0))
     99         printf("Yes
    ");
    100     else
    101         printf("No
    ");
    102     
    103     return 0;
    104 }

  • 相关阅读:
    javascript中有关this的解析题
    变量声明
    js事件
    js用法
    dom对象
    数据的三大储存格式
    函数
    全局环境
    循环语句及案例
    条件语句
  • 原文地址:https://www.cnblogs.com/sankexin/p/5620761.html
Copyright © 2011-2022 走看看