首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。如果是返回true,否则返回false。 例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果: 因此返回true。 如果输入7、4、6、5,没有哪棵树的后序遍历
输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。如果是返回true,否则返回false。 例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果: 因此返回true。 如果输入7、4、6、5,没有哪棵树的后序遍历
admin
2019-03-29
129
问题
输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。如果是返回true,否则返回false。
例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:
因此返回true。
如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。
选项
答案
using namespace std; /////////////////////////////////////////////////////////////////////// // Verify whether a squence of integers are the post order traversal // of a binary search tree (BST) // Input: squence - the squence of integers // length - the length of squence // Return: return ture if the squence is traversal result of a BST, // otherwise, return false /////////////////////////////////////////////////////////////////////// bool verifySquenceOfBST(int squence[], int length) { if(squence == NULL || length <= 0) return false; // root of a BST is at the end of post order traversal squence int root = squence[length - 1]; // the nodes in left sub-tree are less than the root int i = 0; for(; i < length - 1; ++ i) { if(squence[i] > root) break; } // the nodes in the right sub-tree are greater than the root int j = i; for(; j < length - 1; ++ j) { if(squence[j] < root) return false; } // verify whether the left sub-tree is a BST bool left = true; if(i > 0) left = verifySquenceOfBST(squence, i); // verify whether the right sub-tree is a BST bool right = true; if(i < length - 1) right = verifySquenceOfBST(squence + i, length - i - 1); return (left && right); }
解析
这是一道trilogy的笔试题,主要考查对二元查找树的理解。
在后续遍历得到的序列中,最后一个元素为树的根结点。从头开始扫描这个序列,比根结点小的元素都应该位于序列的左半部分;从第一个大于跟结点开始到跟结点前面的一个元素为止,所有元素都应该大于跟结点,因为这部分元素对应的是树的右子树。根据这样的划分,把序列划分为左右两部分,我们递归地确认序列的左、右两部分是不是都是二元查找树。
转载请注明原文地址:https://kaotiyun.com/show/BxmZ777K
0
程序员面试
相关试题推荐
Thecommitteehasanticipatedtheproblemsthat______intheroadconstructionproject.
Theimmunesystemisequalincomplexitytothecombinedintricaciesofthebrainandnervoussystem.Thesuccessoftheimmune
TheUnitedStatesInterstateHighwaySystemisaninfrastructurefeatofunprecedentedproportions.Notonlydoesitjoinallfi
[A]Theperson-skillsmatchapproachtoselection[B]Theimpactsofbadselectiondecisions[C]Theimportanceofstructu
概述反射和序列化
求最大连续递增数字串(如“ads3sl456789DF3456ld345AA”中的“456789”)
输入一个正数n,输出所有和为n连续正数序列。例如输入15,由于1+2+3+4+5=4+5+6=7+8=15,所以输出3个连续序列1-5、4-6和7-8。
将金山网镖应用程序规则列表中的应用程序QQ2009删除。
信息系统的生命周期可以简化为立项、开发、运维及消亡四个阶段,()属于开发阶段的工作。
病毒和木马的根本区别是______。
随机试题
I【C1】______untilthevice-presidentwentbacktohisofficeandknockedonhisdoor.SinceItaughtcollaboration,Idecidedto
计算机中,一个浮点数由两部分组成,它们是阶码和()。
不属于M受体阻滞剂的药物是
李某和朱某的借贷纠纷案件适用了简易程序来审理,法院当场对原告和一同应诉的被告进行了听审,被告认可了原告的一切请求事实,遂无须质证法院就作出了支持原告诉讼请求的判决,案件只用了3天竟然就审理结束。关于本案的审理过程,你认为下列说法错误的有()
根据《劳动合同法》,劳动者可以解除劳动合同,用人单位应当向劳动者支付经济补偿的有()。
应收票据是指企业销售商品、提供劳务等收到的( )。
Itisreasonableforpeopletopursueacareerinfieldsrelate______theirfavoritehobbies.
下列有关会所的分类,说法正确的是()
下列程序实现了矩阵乘法。intA[100][150];intB[150][200];intC[100][200];for(i=0,i
BSP方法实施后的最终成果是向企业最高管理部门提交的【】。
最新回复
(
0
)