题目

输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出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. Beam内置的数据源清单(Java、Python)

    不多说,直接上干货! Beam内置的Java数据源清单: Beam内置的Python数据源清单:  

  2. HDU 3829——Cat VS Dog——————【最大独立集】

    Cat VS Dog Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit S ...

  3. DataGridView带图标的单元格实现

    目的: 扩展 C# WinForm 自带的表格控件,使其可以自动判断数据的上下界限值,并标识溢出. 这里使用的方法是:扩展 表格的列 对象:DataGridViewColumn. 1.创建类:Data ...

  4. ubuntu .net core The specified framework 'Microsoft.NETCore.App', version '1.0.1' was not found

    想在ubuntu下试试.net core mvc,按照官方教程走完,然后把在window 下做好的项目想在ubuntu下试试,然后输入了 git clone https://github.com/ka ...

  5. 未能解析引用的程序集......因为它对不在当前目标框架“.NETFramework,Version=v4.0,Profile=Client”中的

    解决方法:资源管理器下点击项目名(右键)属性--将.NET Framework 4 Client Profile改成.NET Framework 4 . 传送门:http://bbs.csdn.net ...

  6. Spring课程 Spring入门篇 2-2 Spring注入方式

    课程链接: 本节主要讲了以下两块内容: 1 xml两种注入方式 2 注入方式代码实现 3 特别注意 1 xml两种注入方式 构造注入和set注入 2 注入方式代码实现 2.1 set注入方式的实现 实 ...

  7. hibernate课程 初探一对多映射2-3 创建hibernateUtil工具类

    本节主要内容:创建hibernateUtil工具类:demo demo: HibernateUtil.java package hibernate_001; import org.hibernate. ...

  8. css3之图形绘制

    由于近期的项目中出现了不规则的边框和图形, 所以重新温习一下CSS3的图形绘制...样式绘制的图形比图片的性能要好,体验更佳,关键一点是更加有趣! 以下几个例子主要是运用了css3中border.bo ...

  9. aliyun maven repository

    <mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> & ...

  10. DIV内数据删除操作

    对于数据操作,前端提供静态方法,交给后台去操作 此处记录一下,待优化,不过精华都在里面了 静态页面: 鼠标移上显示: html代码 css代码 js代码