zoukankan      html  css  js  c++  java
  • 平衡二叉树实例--判断输入后序序列是否为平衡二叉树

     1 bool verifyPostorder(vector<int>& postorder){
     2 if(postorder.empty())  return true;
     3 bool res = helper(postorder,0,postorder.sizee()-1);
     4 return res;
     5 }
     6 
     7 bool  helper(vector<int>& postorder,int start,int end){
     8 if(postorder.empty() || start>end)  return false;
     9 
    10 int root = postorder[end];
    11 int i = start;
    12 
    13 for(;i<end;i++){
    14 if(postorder[i]<root)
    15 break;
    16 }
    17 for(int j = i;j<end;j++){
    18 if(postorder[j]<root)
    19 return false;
    20 }
    21 bool left = true;
    22 if(i>start)//是否有左子鼠
    23 left = helper(postorder,start,i-1);
    24 bool right = true;
    25 if(i<end-1)//是否有柚子树
    26 right = helper(postorder,i,end-1);
    27 }
    /*
    确定根节点root;
    遍历序列(除去root结点),找到第一个大于root的位置,则该位置左边为左子树,右边为右子树;
    遍历右子树,若发现有小于root的值,则直接返回false;
    分别判断左子树和右子树是否仍是二叉搜索树(即递归步骤1、2、3)。
    */
  • 相关阅读:
    HDU 5937 Equation
    HDU 5936 Difference
    hdu 4348 To the moon
    SPOJ QTREE Query on a tree
    HDU 3966 Aragorn's Story
    Codeforces 733F Drivers Dissatisfaction
    道良心题
    dp小总结
    数据结构
    数学相关(偏数学向题目的集中地)
  • 原文地址:https://www.cnblogs.com/pengtangtang/p/12990242.html
Copyright © 2011-2022 走看看