首页 > 其他 > 详细

平衡二叉树实例--判断输入后序序列是否为平衡二叉树

时间:2020-05-30 01:57:14      阅读:62      评论:0      收藏:0      [点我收藏+]
 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)。
*/

平衡二叉树实例--判断输入后序序列是否为平衡二叉树

原文:https://www.cnblogs.com/pengtangtang/p/12990242.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!