面试题精选(76):给定BST先序遍历序列,不构造BST的情况下判断BST是否每个node都只有一个child
2009-06-12 12:25
603 查看
题目描述:
Input : You have been given a sequence of integers.
Now without actually constructing BST from the given sequence of integers (assuming the sequence is pre-order) determine if each node of BST has single child (either left or right but not both).
Output : YES if given integer sequence corresponds to such a BST. otherwise say NO.
Ex: Say NO for 16,10,8,9,29,27,22.
Say YES for 10,19,17,14,15,16
思路分析:
// Obviously if each node has just one child, it must be a list.
// And it's also a path in a decision tree; so each next number must
// be in between it's maximum smaller ancestor and minimum larger ancestor.
// 16,10,8,9,29,27,22
// =>
// -inf < 10,8,9,29,27,22 < 16
// =>
// -inf < 8,9,29,27,22 < 10
// =>
// 8 < 9,29,27,22 < 10
// =>
// 9 < 29,27,22 < 10
// => failure
//
//
// 10,19,17,14,15,16
// =>
// 10 < 19,17,14,15,16 < inf
// =>
// 10 < 17,14,15,16 < 19
// =>
// 10 < 14,15,16 < 17
// =>
// 14 < 15,16 < 17
// =>
// 15 < 16 < 17
// => pass
代码:
template <typename T>
bool IsOneChildPreorderBST(T seq[],int n)
{
T _max,_min;
assert(n>=2);
if(seq[0]<=seq[1])
{
_max=seq[1]+1;
_min=seq[0]-1;
}
else
{
_max=seq[0]+1;
_min=seq[1]-1;
}
for(int i=1;i<n;i++)
{
if(seq[i]>_max || seq[i]<_min)
return false;
if(seq[i]<seq[i-1])
_max=seq[i-1];
else
_min=seq[i-1];
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
int test1[]={16,10,8,9,29,27,22}; ////NO
int test2[]={10,19,17,14,15,16}; ////YES
if(IsOneChildPreorderBST(test1,7))
cout<<"test1 : YES!/n";
else
cout<<"test1 : NO!/n";
if(IsOneChildPreorderBST(test2,6))
cout<<"test2 : YES!/n";
else
cout<<"test2 : NO!/n";
system("pause");
return 0;
}
Input : You have been given a sequence of integers.
Now without actually constructing BST from the given sequence of integers (assuming the sequence is pre-order) determine if each node of BST has single child (either left or right but not both).
Output : YES if given integer sequence corresponds to such a BST. otherwise say NO.
Ex: Say NO for 16,10,8,9,29,27,22.
Say YES for 10,19,17,14,15,16
思路分析:
// Obviously if each node has just one child, it must be a list.
// And it's also a path in a decision tree; so each next number must
// be in between it's maximum smaller ancestor and minimum larger ancestor.
// 16,10,8,9,29,27,22
// =>
// -inf < 10,8,9,29,27,22 < 16
// =>
// -inf < 8,9,29,27,22 < 10
// =>
// 8 < 9,29,27,22 < 10
// =>
// 9 < 29,27,22 < 10
// => failure
//
//
// 10,19,17,14,15,16
// =>
// 10 < 19,17,14,15,16 < inf
// =>
// 10 < 17,14,15,16 < 19
// =>
// 10 < 14,15,16 < 17
// =>
// 14 < 15,16 < 17
// =>
// 15 < 16 < 17
// => pass
代码:
template <typename T>
bool IsOneChildPreorderBST(T seq[],int n)
{
T _max,_min;
assert(n>=2);
if(seq[0]<=seq[1])
{
_max=seq[1]+1;
_min=seq[0]-1;
}
else
{
_max=seq[0]+1;
_min=seq[1]-1;
}
for(int i=1;i<n;i++)
{
if(seq[i]>_max || seq[i]<_min)
return false;
if(seq[i]<seq[i-1])
_max=seq[i-1];
else
_min=seq[i-1];
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
int test1[]={16,10,8,9,29,27,22}; ////NO
int test2[]={10,19,17,14,15,16}; ////YES
if(IsOneChildPreorderBST(test1,7))
cout<<"test1 : YES!/n";
else
cout<<"test1 : NO!/n";
if(IsOneChildPreorderBST(test2,6))
cout<<"test2 : YES!/n";
else
cout<<"test2 : NO!/n";
system("pause");
return 0;
}
相关文章推荐
- 给定BST先序遍历序列,不构造BST的情况下判断BST是否每个node都只有一个child
- 面试题:给定一个长度为N的数组,其中每个元素的取值范围都是1到N。判断数组中是否有重复的数字
- pat 判断给定插入序列是否为BST的先序遍历序列
- 给定入栈顺序,输出所有可能的出栈情况,并判断给定的序列是否为正确的输出序列
- 【华为面试题】给定一个数字(整数),判断它是否回文
- 面试题:正整数序列Q中的每个元素都至少能被正整数a和b中的一个整除,给定a和b,需要计算出Q中的前几项
- 经典算法 | 给定n个矩形,判断这些矩形是否在不重合的情况下组成一个大矩形的算法
- 【面试题】剑指offer24--判断一个序列是否为二叉树的后序遍历
- 判断一个序列是否是另一个给定序列的弹出序列
- PAT (Advanced Level) 1043. Is It a Binary Search Tree (25) 判断序列是否为BST的先序遍历,递归
- 堆栈面试题之共享栈,最小栈,判断一个序列是否由一个栈进行基本的入出栈操作而得到
- 经典面试题:求数组的最大子序列和;实现函数 :判断一个自负喜欢是否是另一个字符串旋转所得;杨氏矩阵中查找一个数。
- 判断给定的序列是否是一棵BST的后序遍历序列
- 判断一个数据序列是否是BST后序遍历的结果
- 判断给定序列是否为某BST后序输出
- 【100题】给定入栈序列,判断一个序列是否可能为输出序列
- 【面试题】剑指offer22-判断一个序列是否是某一个栈的序列