题目

输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出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. Day3监督学习——决策树原理

    Day3 机器学习监督学习——决策树原理 一.决策树的原理 1.机器学习中分类和预测算法的评估: 准确率 速度 强壮型:有数据缺失或错误时算法的运行 可规模性:数量级规模比较大 可解释性 2.决策树( ...

  2. 导出csv文件时韩文乱码解决方法

    从asp.net导出csv这样配置可以防止韩文等乱码,在头部加上0xEF, 0xBB, 0xBF: string fileName = "attachment;filename=" ...

  3. ruby逻辑判断符号

    puts true and false  #相当于 (puts true) and false Use &&/|| for boolean expressions, and/or fo ...

  4. verilog if语句

    a.基本形式 1) if(表达式) 语句1: 2)if(表达式) 语句1: else 语句1 3) if(表达式1) 语句1: else   if(表达式2) 语句2: else   if(表达式3) ...

  5. C/C++中 static 的作用

    在C中,有三个作用: 1.修饰全局变量: 作用是隐藏,也就是这个全局变量仅在本文件中可见. 2.修饰局部变量: 作用是扩展变量的生存期,令这个局部变量成为静态的. 3.修饰函数: 作用是隐藏,将此函数 ...

  6. 判断表单中是否含有disabled属性

    我想判断input里面是否有disabled.或者选中未选中的selected  checked 属性时,需要用  prop()  方法,返回的结果是 true 或 false . attr()这个方 ...

  7. Aspose.Words导出图片 表格 Interop.Word

    先定义一个WORD 模板, 然后替换文本.域 ,定位开始表格 文本和段落 // Specify font formatting Aspose.Words.Font font = builder.Fon ...

  8. iOS开发实践之多线程(单例模式)

    单例模式的作用:可以保证在程序运行过程,一个类只有一个实例,而且该实例易于供外界访问,从而方便地控制了实例个数,并节约系统资源. 单例模式的使用场合:在这个应用程序中,共享一份资源(这份资源只需要创建 ...

  9. Mysql慢查询 [第二篇]

    一.简介 pt-query-digest是用于分析mysql慢查询的一个工具,它可以分析binlog.General log.slowlog,也可以通过SHOWPROCESSLIST或者通过tcpdu ...

  10. (C# 基础) 接口

    接口是表示一组函数成员,而不实现成员的引用类型.类和结构可以实现接口. 例如BCL声明了一个叫做IComparable的接口,包含了一个CompareTo方法, 但没有实现其方法,用“:”结尾. pu ...