检查一个数组是否可能是一棵二叉查询树后序遍历的结果 [No. 6]
2011-12-14 13:39
267 查看
问题:
给一个数组,检查它是否可能是一棵二叉查询树后序遍历的结果。换句话说,是否存在一棵二叉查询树,对它进行后序遍历后,得到的数组和给定的数组完全一样。
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/03/7126f153a7c0e871114eaed2336b1eb6.gif)
比如,对上面这个二叉查询树后序遍历后,得到的数组是:[1, 4, 7, 6, 3, 13, 14, 10, 8]。
思路:
因为二叉树后序遍历的时候,先左后右然后中间。所以,数组的最后一个一定是整个二叉查询树的根(比如 8),而且,数组前一部分比数组最后一个值(也就是二叉查询树的根)小,因为它们是根的左子树部分。数组剩余部分,是根的右子树部分。而且,在剩余部分里,所有的值都必须比根要大。这样,我们可以把原来的数组分成两个部分,然后每一个部分可以进行递归判断,直到数组的长度小于等于2。(因为当长度小于等于2时,什么样的情况都是可以的)。
我们先把给定的数组分成两个部分,即我们需要先找到数组的分界点。代码如下:
/**
*
* @param array the input array
* @param begin the starting index of the array
* @param end the ending index of the array
* @return the turning point of the array, where the left part of turning point is smaller than array[end], the right
* part of the turning point is larger than array[end].
*/
public int turningPoint(int[] array, int begin, int end) {
int tp = -1;
for (int i = begin; i < end; i++) {
if (array[i] > array[end]) {
tp = i;
break;
}
}
return tp;
}
找到分界点后,我们还要判断从分界点开始,到数组末,是否所有的值都比数组的最后一个值(也就是根)大,否则,这样的数组不是一个二叉查询树的后序遍历。代码如下:
//check whether all the elements from strat are larger than array[end]
public boolean checkValid(int[] array, int start, int end) {
for (int i = start; i < end; i++) {
if (array[i] < array[end]) return false;
}
return true;
}
有了上面两个方法,我们就可以把递归写出来了,代码如下:
// the main method to check whether the array is identical to a BST's post order array
public boolean isPostOrder(int[] array, int begin, int end) {
// the exit
if ((end - begin + 1) <= 2) return true;
//get the turning point
int turningPoint = turningPoint(array, begin, end);
boolean result = checkValid(array, turningPoint, end); //check whether the right part of the array is valid
if (result == false) {
return false;
}
// recursion
return isPostOrder(array, begin, turningPoint - 1) && isPostOrder(array, turningPoint, end - 1);
}
给一个数组,检查它是否可能是一棵二叉查询树后序遍历的结果。换句话说,是否存在一棵二叉查询树,对它进行后序遍历后,得到的数组和给定的数组完全一样。
![](https://oscdn.geek-share.com/Uploads/Images/Content/202004/03/7126f153a7c0e871114eaed2336b1eb6.gif)
比如,对上面这个二叉查询树后序遍历后,得到的数组是:[1, 4, 7, 6, 3, 13, 14, 10, 8]。
思路:
因为二叉树后序遍历的时候,先左后右然后中间。所以,数组的最后一个一定是整个二叉查询树的根(比如 8),而且,数组前一部分比数组最后一个值(也就是二叉查询树的根)小,因为它们是根的左子树部分。数组剩余部分,是根的右子树部分。而且,在剩余部分里,所有的值都必须比根要大。这样,我们可以把原来的数组分成两个部分,然后每一个部分可以进行递归判断,直到数组的长度小于等于2。(因为当长度小于等于2时,什么样的情况都是可以的)。
我们先把给定的数组分成两个部分,即我们需要先找到数组的分界点。代码如下:
/**
*
* @param array the input array
* @param begin the starting index of the array
* @param end the ending index of the array
* @return the turning point of the array, where the left part of turning point is smaller than array[end], the right
* part of the turning point is larger than array[end].
*/
public int turningPoint(int[] array, int begin, int end) {
int tp = -1;
for (int i = begin; i < end; i++) {
if (array[i] > array[end]) {
tp = i;
break;
}
}
return tp;
}
找到分界点后,我们还要判断从分界点开始,到数组末,是否所有的值都比数组的最后一个值(也就是根)大,否则,这样的数组不是一个二叉查询树的后序遍历。代码如下:
//check whether all the elements from strat are larger than array[end]
public boolean checkValid(int[] array, int start, int end) {
for (int i = start; i < end; i++) {
if (array[i] < array[end]) return false;
}
return true;
}
有了上面两个方法,我们就可以把递归写出来了,代码如下:
// the main method to check whether the array is identical to a BST's post order array
public boolean isPostOrder(int[] array, int begin, int end) {
// the exit
if ((end - begin + 1) <= 2) return true;
//get the turning point
int turningPoint = turningPoint(array, begin, end);
boolean result = checkValid(array, turningPoint, end); //check whether the right part of the array is valid
if (result == false) {
return false;
}
// recursion
return isPostOrder(array, begin, turningPoint - 1) && isPostOrder(array, turningPoint, end - 1);
}
相关文章推荐
- 检查一个数组是否可能是一棵二叉查询树后序遍历的结果
- 输入一个数组,判断该数组是否是某二叉树的前序遍历结果
- 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
- 《剑指Offer》输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
- 判断一个数组是否为后序遍历结果
- 【剑指offer24】输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
- 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
- java 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
- 数据结构:题目(3)测试一个数组序列是否是二叉树的前序遍历或者后序遍历结果
- 把一个排好序的数组数组转化成一颗平衡二叉查询树 [No. 79]
- 1367判断一个数组是否为二叉排序树的后序遍历结果
- 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
- 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
- 如何高效检查一个数组中是否包含某个值?
- 给定一个向量/数组,判定该向量/数组是否为一个二叉搜索数的后续遍历C++
- 判断给定数组是否是二叉树的前序或者后序遍历结果
- NSArray不用遍历能否判断一个对象是否存在在数组里面?
- java 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果
- Python实遍历二维数组题目一:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
- 检查一个后序遍历是否是搜索二叉树