剑指Offer_23_二叉搜索树的后序遍历序列
来源:互联网 发布:全球网络电视直播软件 编辑:程序博客网 时间:2024/06/16 13:19
题目描述
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
解题思路
首先找到树的根结点,然后找出比根结点小的和比根结点大的元素范围,若符合要求,则分别是根结点的左右子树,然后继续递归判断。
实现
public class Solution { public boolean VerifySquenceOfBST(int [] sequence) { if (sequence == null || sequence.length <= 0) return true; return VerifySquenceOfBSTRecursion(sequence, 0 ,sequence.length - 1); } private boolean VerifySquenceOfBSTRecursion(int[] sequence, int start, int end) { if (start>=end) return true; int root = sequence[end]; int endLeft = start - 1; boolean isInLeft = true; for(int i = start; i < end; i++){ if (sequence[i] < root && isInLeft){ endLeft = i; }else if (sequence[i] < root && !isInLeft) return false; else if (sequence[i] > root) isInLeft = false; } return VerifySquenceOfBSTRecursion(sequence, start, endLeft) && VerifySquenceOfBSTRecursion(sequence, endLeft + 1, end - 1); }}
0 0
- 剑指Offer_23_二叉搜索树的后序遍历序列
- 剑指offer:二叉搜索树的后序遍历序列
- 【剑指offer】二叉搜索树的后序遍历序列
- 剑指offer--二叉搜索树的后序遍历序列
- 剑指Offer-->二叉搜索树的后序遍历序列
- 《剑指offer》二叉搜索树的后序遍历序列
- 【剑指Offer】二叉搜索树的后序遍历序列
- 剑指offer--二叉搜索树的后序遍历序列
- 剑指 offer:二叉搜索树的后序遍历序列
- 剑指offer:二叉搜索树的后序遍历序列
- 【剑指offer】二叉搜索树的后序遍历序列
- 二叉搜索树的后序遍历序列||剑指offer
- 剑指offer-二叉搜索树的后序遍历序列
- 剑指offer 二叉搜索树的后序遍历序列
- 剑指offer 二叉搜索树的后序遍历序列
- 剑指offer:二叉搜索树的后序遍历序列
- [剑指offer]二叉搜索树的后序遍历序列
- 《剑指offer》-二叉搜索树的后序遍历序列
- ESP8266自定义用户目录
- ubuntu下对文件的读写操作
- 阻塞队列详解
- java IO流
- sockaddr_in , sockaddr , in_addr区别
- 剑指Offer_23_二叉搜索树的后序遍历序列
- director知识点
- Deep Hashing: A Joint Approach for Image Signature Learning阅读笔记
- nice validator ajax
- nginx的常用命令
- 背包九讲
- Excel去掉函数错误值#VALUE!的方法
- HDU 1695 GCD (莫比乌斯反演)
- java与C++的区别