Given a binary tree, determine if it is a valid binary search tree (BST).

Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • Both the left and right subtrees must also be binary search trees.

这是easy题么=_= 被虐得好惨,开始想得简单,不就是验证BST咩,递归的话只用判断左右孩子是不是BST就行了。。。这就错了,孩子是BST并不能保证整颗树就是BST啊。比如下图

              5

              /    \

             3     6

             /   \  /  \

           1   8 4  7

5的左右孩子都分别是合法的BST,但是整颗树却不是,因为6的左孩子比5小;3的右孩子比5大。

所以在递归检测的时候,不仅仅要保证当前的树是BST,也要保证当前的树不破坏原有的BST结构,怎么办呢?

认真思考BST的结构性质会发现,BST就好像把一串数字做了很多切分,一部分放左变一部分放右边,一直这样递归的切下去。而这个切法可不是乱切,他是永远保持 “小的放左边,大的放右边” 的,这样切就有个性质,就是被切出来的每一段区域,都有严格的上下界。

              3

              /    \

             1     5

             /   \  /  \

           0   2 4  6

上图是一颗合法的BST,如果我们将其按照中序遍历方式展开可得:0 1 2 3 4 5 6

注意观察1,3,5 把序列切分成了4个区域,每个区域有一个数子分别是0,2,4,6。除了0没有下界,6没有上界以外,2和4都有明确的上下界。

所以在递归检测当中,我们应该同时传递上下界信息。

思路:在递归检测左右孩子的同时传递上下界信息,在递归调过程中更新上下界信息。

举个栗子:以上图为例,以根节点启动算法时,上下界信息为空,在检测1,3,5这个结构是否为合法BST后,递归的检测根节点为1的树(左孩子)并传入上界信息3;同样,递归检测根节点为5的树(右孩子)并传入下界信息3...

在递归过程中更新上下界信息,比如在检测到2这个节点的时候,更新了下界为1,而上界不用更新还是3。

bool validIter(TreeNode *node, TreeNode *left, TreeNode *right) {
if (!node) return true;
bool validLeft = true;
bool validRight = true;
if (node->left) {
validLeft = (node->val > node->left->val);
if (left) {
validLeft = validLeft && (node->left->val > left->val);
}
} if (node->right) {
validRight = (node->val < node->right->val);
if (right) {
validRight = validRight && (node->right->val < right->val);
}
} if (validLeft && validRight) {
return validIter(node->left, left, node) && validIter(node->right, node, right);
}
return false;
} bool isValidBST(TreeNode *root) {
return validIter(root, NULL, NULL);
}

要点:检测当前树是否为BST的时候还要检测是否违反了上下界的约束。

解法二:使用中序便利

上面的递归解法不太好想,我跌跌撞撞的提交了两次才对算法有了较深的理解。前面也说到,BST按照中序便利的话将会产生一个有序的序列,这是BST的性质。那为何不利用这个性质:先中序遍历产生数组,然后检查数组是否有序和是否有重复。

这个思路就是这么简单。。。中序遍历然后检查数组,所以才是easy题啊 (逃

void midOrderTraversal(TreeNode *node, vector<int> &s) {
if (!node) return;
midOrderTraversal(node->left, s);
s.push_back(node->val);
midOrderTraversal(node->right, s);
} bool isValidBST(TreeNode *root) {
if (!root) return true;
vector<int> s;
midOrderTraversal(root, s); for (int i = ; i < s.size(); i++) {
if (s[i] < s[i-] || s[i] == s[i-]) {
return false;
}
}
return true;
}

[LeetCode] Validate Binary Search Tree (两种解法)的更多相关文章

  1. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  2. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  3. Leetcode Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  4. LeetCode: Validate Binary Search Tree [098]

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  5. LeetCode :: Validate Binary Search Tree[具体分析]

    Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less th ...

  6. [leetcode]Validate Binary Search Tree @ Python

    原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/ 题意:检测一颗二叉树是否是二叉查找树. 解题思路:看到二叉树我们首 ...

  7. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  8. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  9. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

随机推荐

  1. 5-python学习——条件语句

    5-python学习——条件语句 5-python学习——条件语句 条件语句if else形式 if else条件语句说明 测试一下 编程语言一般都由这么几个部分组成 变量 条件分支语句 循环语句 函 ...

  2. peewee 字段属性help_text的支持问题

    至少在__version__ = '2.6.0'的时候,给字段添加help_text的时候,在数据库的ddl语句里面是没有comment的. 看了下源码,顺藤摸瓜,最终定位到了字段(Field类)的_ ...

  3. Docker的安装配置及使用详解

    基本概念 Docker 包括三个基本概念 镜像(Image) 容器(Container) 仓库(Repository) 先理解了这三个概念,就理解了 Docker 的整个生命周期. 1.docker安 ...

  4. 自动化运维工具ansible学习+使用ansible批量推送公钥到远程主机

    目录: 一.ansible简介 1.1.ansible是什么 1.2.ansible如何工作 1.3.ansible优缺点 1.4.ansible安装方式 1.5.ansible文件简单介绍 1.6. ...

  5. 设定报表变量的CharSpacing

    设定报表变量的CharSpacing字符间距,预览时都没问题, 间距大的字与字之间拉得比较大,但在大多数电脑打印时和预览的结果一样,但有些电脑打印出来却跟没有设间距一样?

  6. ajax与后台交互传输数据的工具类

    public class Result<T> implements Serializable { private static final long serialVersionUID = ...

  7. 修改UINavigationController返回按钮颜色

    系统默认颜色是蓝色的 视觉效果非常难看 在push进的ViewController中写 //修改UINavigationController的文字颜色 self.navigationControlle ...

  8. 针对SYN洪水攻击的防御措施

    可以运用sysctl命令进行配置,由于本命令参数较多,这里只简单记录几个比较常用的参数: 1.tcp_max_syn_backlog 这个参数指定了后备队列可维持的TCP半开连接的数目,如果该值设定很 ...

  9. 【linux】find删除指定时间之前的文件

    今天磁盘满了,想删掉一些老的日志文件.开始想写个python脚本,转念一想,可能shell脚本好点.结果发现,根本不用写脚本,一个find指令就可以解决问题了. 先上指令 -exec rm {} \; ...

  10. HDU 5901 Count primes (1e11内的素数个数) -2016 ICPC沈阳赛区网络赛

    题目链接 题意:求[1,n]有多少个素数,1<=n<=10^11.时限为6000ms. 官方题解:一个模板题, 具体方法参考wiki或者Four Divisors. 题解:给出两种代码. ...