剑指offer 24. 二叉搜索树的后序遍历序列
来源:互联网 发布:十代思域18寸轮毂数据 编辑:程序博客网 时间:2024/05/20 16:32
// 题目:输入一个数组,判断是不是二叉搜索树的后序遍历结果// 解法:最后一个结点是根结点,前面所有结点小于根节点,后面所有节点大于根节点值public class Main {public static void main(String[] args) {System.out.println(checkSeq(new int[] { 5,7,9,11,10,8}));}private static boolean checkSeq(int[] input) {if(input.length <= 0){return false;}int cut = -1;int value = input[input.length-1];for(int i = 0;i<input.length-1;i++){//找到左子树和右子树的分界点if(input[i]>=value){cut = i;break;}}if(cut == -1){//如果只有左子树则直接成功return true;}for(int i = cut;i<input.length-1;i++){//如果右子树有结点比根节点小,则直接返回falseif(input[i]<=value){return false;}}boolean left = true;if(cut>0){left = checkSeq(Arrays.copyOfRange(input, 0, cut));//考虑左边的数组}boolean right = true;if(cut<input.length-1){right = checkSeq(Arrays.copyOfRange(input, cut, input.length-1));//检查右边的数组}return left && right;}}
0 0
- 剑指offer:二叉搜索树的后序遍历序列
- 【剑指offer】二叉搜索树的后序遍历序列
- 剑指offer--二叉搜索树的后序遍历序列
- 剑指Offer-->二叉搜索树的后序遍历序列
- 《剑指offer》二叉搜索树的后序遍历序列
- 【剑指Offer】二叉搜索树的后序遍历序列
- 剑指offer--二叉搜索树的后序遍历序列
- 剑指 offer:二叉搜索树的后序遍历序列
- 剑指offer:二叉搜索树的后序遍历序列
- 【剑指offer】二叉搜索树的后序遍历序列
- 二叉搜索树的后序遍历序列||剑指offer
- 剑指offer-二叉搜索树的后序遍历序列
- 剑指offer 二叉搜索树的后序遍历序列
- 剑指offer 二叉搜索树的后序遍历序列
- 剑指offer:二叉搜索树的后序遍历序列
- [剑指offer]二叉搜索树的后序遍历序列
- 《剑指offer》-二叉搜索树的后序遍历序列
- 【剑指offer】二叉搜索树的后序遍历序列
- hiho ! 1039 : 字符消除
- CentOS设置静态IP
- 网络编程三要素整理
- 1079. Total Sales of Supply Chain 解析
- 搭建 Asp.net 云服务器
- 剑指offer 24. 二叉搜索树的后序遍历序列
- Enum(枚举类型)的基本应用
- Linux系统中SHELL命令或脚本中关于$理解
- IGN评史上最佳100 RPG
- 2017/5/9
- redis 安装以及phpredis拓展安装
- ajax访问后台,数据放到map中,map明明有值, 前台获取的为null
- 机器学习模型优化中常见问题和解决思路
- HTTP2.0 HTTPS学习