笔试面试之判断序列是否2叉查找树后序遍历
2010-06-16 15:30
218 查看
输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。如果是返回true,否则返回false。
例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:
8
/ /
6 10
/ / / /
5 7 9 11
因此返回true。
如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。
分析:这是一道trilogy的笔试题,主要考查对二元查找树的理解。
在后续遍历得到的序列中,最后一个元素为树的根结点。从头开始扫描这个序列,比根结点小的元素都应该位于序列的左半部分;从第一个大于跟结点开始到跟结点前面的一个元素为止,所有元素都应该大于跟结点,因为这部分元素对应的是树的右子树。根据这样的划分,把序列划分为左右两部分,我们递归地确认序列的左、右两部分是不是都是二元查找树。
参考代码:
using namespace std;
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);
}
转载自:http://zhedahht.blog.163.com/blog/static/25411174200725319627/
例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:
8
/ /
6 10
/ / / /
5 7 9 11
因此返回true。
如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。
分析:这是一道trilogy的笔试题,主要考查对二元查找树的理解。
在后续遍历得到的序列中,最后一个元素为树的根结点。从头开始扫描这个序列,比根结点小的元素都应该位于序列的左半部分;从第一个大于跟结点开始到跟结点前面的一个元素为止,所有元素都应该大于跟结点,因为这部分元素对应的是树的右子树。根据这样的划分,把序列划分为左右两部分,我们递归地确认序列的左、右两部分是不是都是二元查找树。
参考代码:
using namespace std;
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);
}
转载自:http://zhedahht.blog.163.com/blog/static/25411174200725319627/
相关文章推荐
- 算法--判断一个数字序列是否为二叉排序树的后续遍历
- 判断链表是否有环以及查找环的入口点——淘宝笔试归来
- 二叉搜索树的后序遍历序列(判断后序遍历序列是否合法)
- 笔试题:判断栈的push(),pop()序列是否正确
- 程序员面试题目总结--数组(五)【数组的后面m个数移动为前面m个数、列的前n项数据、判断整数x是否可以表示成n个连续正整数的和、数组中出现奇数次的元素、二维数组中的查找】
- 剑指offer解题报告(Java版)——判断一个数组是否是二叉搜索树的后序遍历序列 24
- 判断整数序列是否为二元查找树的后序遍历结果的解决方法
- 笔试,面试,C/C++,判断单链表是否带环?若带环,求环长度,求环入口点(两种方法)
- 判断二叉树的后序遍历序列是否合法
- 二叉树——判断两棵二叉树是否相等(先序和中序遍历序列建立二叉树)
- 程序员面试题目总结--数组(二)【二分查找、找出给定数字出现次数、两个有序整型数组交集、找出数组中唯一的重复元素、判断数组中的数值是否连续相邻】
- 笔试面试之将2叉查找树转换为双向链表
- <笔试><面试>C/C++单链表相关(4)判断两链表是否相交,求交点(链表不带环/可能带环)
- 建立有向图的邻接表,深度优先遍历和广度优先遍历的递归与非递归算法,判断是否是有向无环图,并输出一种拓扑序列
- 笔试,面试,C/C++,判断单链表是否带环?若带环,求环长度,求环入口点(两种方法)
- [互联网面试笔试汇总C/C++-15] 判断一棵二叉树是否是完全搜索树-微策略
- 【面试题】剑指offer24--判断一个序列是否为二叉树的后序遍历
- 输入一个数列,判断是否为某一个二叉搜索树的后序遍历序列
- 《剑指offer》:[24]判断一个序列是否为二叉树的后序遍历序列