剑指offer-面试题24-二叉树的后序遍历序列
来源:互联网 发布:stm32跑linux 编辑:程序博客网 时间:2024/06/03 14:04
package case24_VarifyArrIsPostBT;public class VarifyArrIsPostBT {/** * 题目: * 输入一个整数数组,判断该数组是不是某二叉搜索树的后续遍历的结果。如果是返回true,否则返回false。假设输入的数组的任意两个数字都不相同。 * * @param args */// 采用递归的方式public static boolean VarifyPBT(int[] arr) {if (arr == null || arr.length < 1)return false;int low = 0;int high = arr.length - 1;return isPostBinaryTree(arr, low, high);}public static boolean isPostBinaryTree(int[] arr, int low, int high) {if (low >= high)return true;// arr序列的最后一个数字是后续遍历二叉树的根节点int root = arr[high];int i = low;// 判断 左子树序列 [low,i-1]是左子树for (; i < high; i++) {if (arr[i] > root)break;}// 判断 右子树序列 [i,high-1]是右子树int j = i;for (; j < high; j++) {if (arr[j] < root)return false;}// 递归左子树boolean left = isPostBinaryTree(arr, low, i - 1);// 递归右子树boolean right = isPostBinaryTree(arr, i, high - 1);return left && right;}// ****test case start*****//public static void main(String[] args) {// 10// / \// 6 14// /\ /\// 4 8 12 16int[] data = { 4, 8, 6, 12, 16, 14, 10 };System.out.println("true: " + VarifyPBT(data));// 5// / \// 4 7// /// 6int[] data2 = { 4, 6, 7, 5 };System.out.println("true: " + VarifyPBT(data2));// 5// /// 4// /// 3// /// 2// /// 1int[] data3 = { 1, 2, 3, 4, 5 };System.out.println("true: " + VarifyPBT(data3));// 1// \// 2// \// 3// \// 4// \// 5int[] data4 = { 5, 4, 3, 2, 1 };System.out.println("true: " + VarifyPBT(data4));// 树中只有1个结点int[] data5 = { 5 };System.out.println("true: " + VarifyPBT(data5));int[] data6 = { 7, 4, 6, 5 };System.out.println("false: " + VarifyPBT(data6));int[] data7 = { 4, 6, 12, 8, 16, 14, 10 };System.out.println("false: " + VarifyPBT(data7));}// ****test case end*****//}
0 0
- [剑指offer][面试题24]二叉搜索树的后序遍历序列
- 【剑指offer】面试题24:二叉搜索树的后序遍历序列
- 剑指Offer:面试题24 二叉搜索树的后序遍历序列
- 《剑指Offer》面试题24:二叉搜索树的后序遍历序列
- 剑指offer 面试题24—二叉搜索树的后序遍历序列
- 剑指offer--面试题24:二叉搜索树的后序遍历序列--Java实现
- 【剑指Offer学习】【面试题24:二叉搜索树的后序遍历序列】
- 剑指Offer面试题24(Java版):二叉搜索树的后序遍历序列
- 剑指offer面试题24-二叉搜索树的后序遍历序列
- 剑指offer-面试题24:二叉搜索树的后序遍历序列
- 剑指offer之面试题24:二叉搜索树的后序遍历序列
- 剑指Offer----面试题24:二叉搜索树的后序遍历序列
- 剑指offer-面试题24:二叉搜索树的后序遍历序列
- 剑指offer面试题24:二叉搜索树的后序遍历序列
- 剑指offer面试题24:二叉搜索树的后序遍历序列
- 剑指offer--面试题24: 二叉搜索树的后序遍历序列
- 剑指offer——面试题24:二叉搜索树的后序遍历序列
- 剑指Offer之面试题24:二叉搜索树的后序遍历序列
- WinEdt初次接触
- (jquery)input输入一个日期得到前后几天的日期
- 常用正则表达式
- windos 下 安装 pip
- SBC-B1 TI 335x 启动分析(从哪里加载uboot和 uImage)
- 剑指offer-面试题24-二叉树的后序遍历序列
- (转)Android控件
- bookstrap3常用组件
- mysql主从复制
- 出现 java.rmi.server.ExportException 异常的原因及解决方法
- 图像分割(四)——聚类分析/小波变换
- 1005. 继续(3n+1)猜想
- HTML5 jQuery图片上传前预览
- HDU