题目

输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。


考点

1.BST 二叉搜索树

2.递归


思路

1.后序遍历,根节点是序列的最后一个。

2.BST中左子树的值比根节点小,如果序列第一个数就比根节点大,说明没有左子树,break

3.BST中右子树的值比根节点大,如果右子树有比根节点小的数,说明不是BST,return false

4.递归 left=左子数,right=右子树

5.return left&&right


代码

class Solution {
public:
bool VerifySquenceOfBST(vector<int> sequence) {
//1.入口检查
if(!sequence.size())
return false;
//2.根节点
int root = sequence.back();
auto itLeft=sequence.begin(); //3.是否左子树都比根节点小
for( ;itLeft<(sequence.end()-1);itLeft++)
{
if(*itLeft>root)
break;
}
auto itRight=itLeft;
//4.是否右子树都比根节点大
for(;itRight<(sequence.end()-1);itRight++)
{
if(*itRight<root)
return false;
} bool left=true;
//5.如果存在左子树,递归检查该左子树是否是BST
if(itLeft!=sequence.begin())
{
//检查左子树
vector<int> lefttemp;
for(int i=0;i<(itLeft-sequence.begin());i++)
{
lefttemp.push_back(sequence.front());
}
left=VerifySquenceOfBST(lefttemp);
} bool right=true;
//6.如果存在右子树,递归检查该右子树是否是BST
if(itRight!=itLeft)
{
//将左子树去掉
sequence.erase(sequence.begin(),itLeft);
//去掉根结点
sequence.pop_back();
//检查右子树
left=VerifySquenceOfBST(sequence);
} return left&&right;
}
};

问题

第33题:LeetCode255 Verify Preorder Sequence in Binary Search Tree 验证先序遍历是否符合二叉搜索树的更多相关文章

  1. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  2. [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  3. [Locked] Verify Preorder Sequence in Binary Search Tree

    Verify Preorder Sequence in Binary Search Tree Given an array of numbers, verify whether it is the c ...

  4. [Swift]LeetCode255.验证二叉搜索树的先序序列 $ Verify Preorder Sequence in Binary Search Tree

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  5. LeetCode Verify Preorder Sequence in Binary Search Tree

    原题链接在这里:https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/ 题目: Given an a ...

  6. Leetcode 255. Verify Preorder Sequence in Binary Search Tree

    验证一个list是不是一个BST的preorder traversal sequence. Given an array of numbers, verify whether it is the co ...

  7. 255. Verify Preorder Sequence in Binary Search Tree

    题目: Given an array of numbers, verify whether it is the correct preorder traversal sequence of a bin ...

  8. [LC] 255. Verify Preorder Sequence in Binary Search Tree

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  9. [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree_Medium tag: Preorder Traversal, tree

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

随机推荐

  1. 修改linux文件权限命令

    修改linux文件权限命令:chmod Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作. 文件或目录的访问权限分为只读,只写和可执行三种.以文 ...

  2. Python 科学工具使用

    Python 科学工具笔记 numpy a = numpy.array([1,2,3,4]);// 创建一个numpy的数组对象 此时a.shape显示的值为(4,); 由此得出结论在一维的数组中, ...

  3. GitKraken使用教程-基础部分(2)

    3. 修改用户名 为了方便项目中代码的管理,需要重新编辑用户名. 点击右上角的图像即可看到如下图 3‑1所示的下拉菜单,鼠标悬于Profile上,会出现一个Edit按钮. 图 3‑1 编辑个人信息 点 ...

  4. 把js生成的内容放入网页原有的div上

    <script> ; ; //5列 ); ; var htmlstr="<table style='position:absolute;top:9%;left:10%; b ...

  5. 如何使用JWT来实现单点登录功能

    我们平时自己开发项目,分布式的结构时,访问量不大,但是又不想搭建redis服务器,这时我觉得jwt不错. 个人理解,jwt就是类似于一把锁和钥匙,客户来租房(登录),我们需要给他进来(第一次登录)登记 ...

  6. 使用Ribbon Workbench来修改停用、激活按钮的权限

    在实施的过程中,有时会遇到客户为了管控使用人员的操作或防止使用人员通过停用后再激活来绕开部分逻辑,需要对激活.停用按钮赋予单独的权限.但很遗憾,在Dyanmics CRM中,并没有把停用.激活按钮单独 ...

  7. Android Process & Thread

    Native Service and Android Service Native Service:In every main() method of NativeService, which is ...

  8. 初学:react-native 轮播图

    参考资料:http://reactscript.com/react-native-card-carousel-component/ import React, {Component} from 're ...

  9. rpm打包工具

    http://fedoraproject.org/wiki/How_to_create_an_RPM_package # rpm --showrc|grep _topdir -14: _builddi ...

  10. php网站修改默认访问文件的nginx配置

    搭建好lnmp后,有时候并不需要直接访问index.php,配置其他的默认访问文件比如index.html这时候需要配置一下nginx才能访问到你想要设置的文件 直接上代码,如下是我的配置的一份简单的 ...