二叉搜索树的后序遍历序列(剑指offer)
来源:互联网 发布:苏州市那美网络 编辑:程序博客网 时间:2024/06/03 19:54
二叉搜索树的后序遍历序列
- 参与人数:1407时间限制:1秒空间限制:32768K
- 通过比例:21.19%
- 最佳记录:0 ms|0K(来自 小YI)
题目描述
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
链接:http://www.nowcoder.com/practice/a861533d45854474ac791d90e447bafd?rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
同时由于是后序遍历(左右根)所以,最后的是根节点,那么从前往后搜,搜到第一个大于根节点之前的都是根节点的左子树,那么之后的数应该都是根节点的右子树,也就是说大必须根结点;然后递归判断每个结点是左右子树是否符合条件。。。
class Solution {public: bool VerifySquenceOfBST(vector<int> sequence) { int len=sequence.size(); if(!len) return false; int root=sequence[len-1]; vector<int> ls; vector<int> rs; int i=0; for(;i<len-1;i++) { if(sequence[i]>root) break; ls.push_back(sequence[i]); } //判断右子树,要求都大于root int j=i; for(;j<len-1;j++) { if(sequence[j]<root) return false; rs.push_back(sequence[j]); } //递归判断左右子树 bool left=true; if(ls.size()) left=VerifySquenceOfBST(ls); bool right=true; if(rs.size()) right=VerifySquenceOfBST(rs); return left&&right ; }};
0 0
- 剑指offer:二叉搜索树的后序遍历序列
- 【剑指offer】二叉搜索树的后序遍历序列
- 剑指offer--二叉搜索树的后序遍历序列
- 剑指Offer-->二叉搜索树的后序遍历序列
- 《剑指offer》二叉搜索树的后序遍历序列
- 【剑指Offer】二叉搜索树的后序遍历序列
- 剑指offer--二叉搜索树的后序遍历序列
- 剑指 offer:二叉搜索树的后序遍历序列
- 剑指offer:二叉搜索树的后序遍历序列
- 【剑指offer】二叉搜索树的后序遍历序列
- 二叉搜索树的后序遍历序列||剑指offer
- 剑指offer-二叉搜索树的后序遍历序列
- 剑指offer 二叉搜索树的后序遍历序列
- 剑指offer 二叉搜索树的后序遍历序列
- 剑指offer:二叉搜索树的后序遍历序列
- [剑指offer]二叉搜索树的后序遍历序列
- 《剑指offer》-二叉搜索树的后序遍历序列
- 【剑指offer】二叉搜索树的后序遍历序列
- android四大组件详解
- linux mysql 命令
- Android UI 设计之TitleBar
- PAT(甲级)1025
- Httpsqs类。
- 二叉搜索树的后序遍历序列(剑指offer)
- 杭电2112-HDU Today(最短路)
- Vagrant中搭建Docker虚拟环境(上)-Mac
- 查看oracle当前连接数(转)
- 任务管理器的实现及源码
- spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
- 线程池ThreadPoolExecutor使用简介
- 1、Find the last element of a list.
- 关于JAVA 类加载器的研究