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.

题目大意:给定一个二叉树,判断它是不是二叉搜索树,二叉搜索树满足要求左子树所有的值小于当前节点的值,右子树所有的值大于当前节点,左右子树也分别是二叉搜索树。

解题思路:

一、递归验证,每个节点设置一个允许的范围,根节点当然是所有的都可以,其他的节点要大于min并且小于max,并且递归验证左右子树的时候要更新min和max。

 10 ----- binary tree (0)
/ \
5 15 -------- binary tree (1)
/ \
6 20

如上,tree(1)是合法的,但是6比10小,6所在的位置应该是位于 10~15之间,所以这棵树是不合法的。也就是说,需要两个值max, min 来记录当前节点应该的取值范围。那么min和max的更新符合什么规则呢?对于左子树,应该小于根节点,对于右子树,应该大于根节点;对于一个节点来说,它的值应该是左子树的上限,右子树的下限。

拿上面的树举例来说,节点15的下限是10,上限为null,递归到左子树节点6,下限为10,上限为15,不符合范围要求,返回false。

    public boolean isValidBST(TreeNode root) {
return doValidate(root, null, null);
} private boolean doValidate(TreeNode root, Integer min, Integer max) {
if (root == null) {
return true;
}
int val = root.val;
if (min != null && min >= val) {
return false;
}
if (max != null && max <= val) {
return false;
}
return doValidate(root.left, min, val) && doValidate(root.right, val, max);
}

二、(推荐)直观,清晰的解题思路:二叉搜索树的中序遍历序列应该是递增的,那么只需要检查中序遍历序列是否是递增序的。

    public boolean isValidBST2(TreeNode root) {
if (root == null) {
return true;
}
List<Integer> list = new ArrayList<>();
doValidate(root, list);
for (int i = 1; i < list.size(); i++) {
if (list.get(i - 1) >= list.get(i)) {
return false;
}
}
return true;
} void doValidate(TreeNode node, List<Integer> list) {
if (node == null) {
return;
}
doValidate(node.left, list);
list.add(node.val);
doValidate(node.right, list);
}

Validate Binary Search Tree——LeetCode的更多相关文章

  1. Validate Binary Search Tree [LeetCode]

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

  2. Validate Binary Search Tree leetcode java

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  3. Leetcode 笔记 98 - Validate Binary Search Tree

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

  4. 【leetcode】Validate Binary Search Tree

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

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

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

  6. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

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

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

  8. 【LeetCode】98. Validate Binary Search Tree (2 solutions)

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

  9. 39. Recover Binary Search Tree && Validate Binary Search Tree

    Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...

随机推荐

  1. codevs 3119 高精度练习之大整数开根 (各种高精+压位)

    /* codevs 3119 高精度练习之大整数开根 (各种高精+压位) 二分答案 然后高精判重 打了一个多小时..... 最后还超时了...压位就好了 测试点#1.in 结果:AC 内存使用量: 2 ...

  2. TOKEN的保存与验证

    Token主要为了防止非本页数据的提交,防止重复提交. /** * * 保存TOKEN信息 * */ public void saveToken() { //此处生成md5串 string md5 = ...

  3. HTML5 ArrayBufferView之DataView

    DataView视图 如果一段数据包括多种类型(比如服务器传来的HTTP数据),这时除了建立ArrayBuffer对象的复合视图以外,还可以通过DataView视图进行操作. DataView视图提供 ...

  4. SQL Server死锁日志各字段含义

    使用跟踪标记 1204 --打开跟踪标记 DBCC TRACEON (1204,-1) --关闭跟踪标记 DBCC TRACEOFF (1204,-1) 处于死锁状态时,跟踪标记 1204 在等待的线 ...

  5. Deep Learning 学习随记(四)自学习和非监督特征学习

    接着看讲义,接下来这章应该是Self-Taught Learning and Unsupervised Feature Learning. 含义: 从字面上不难理解其意思.这里的self-taught ...

  6. 开发自己的cordova插件

    如果还没有配置过cordova环境,首先要下载nodejs,(下载地址https://nodejs.org/)下载完毕安装. 控制台: 1.输入npm -v 确定是否装上了 2.输入sudo npm ...

  7. 华硕_ZX50JX4200 安装ssd固态盘

    本人亲身的一次经历,帮朋友的华硕手提装一个内存和ssd固态 内存5分钟搞定,但是ssd固态盘就经过了一番的折腾 首先要拧掉所有后盖的螺丝,把光驱拆下来,注意撬开键盘板的时候有排线,不能弄断了.然后一定 ...

  8. cas的url中去掉jsessionid

    Servlet3.0规范中的<tracking-mode>允许你定义JSESSIONID是存储在cookie中还是URL参数中.如果会话ID存储在URL中,那么它可能会被无意的存储 在多个 ...

  9. Ubuntu14.04+CUDA6.5环境下神经网络工具包Deepnet配置

    deepnet是多伦多大学计算机系机器学习组开发的一个神经网络工具包,可以进行以下计算: 1.  Feed-forward Neural Nets 2.  Restricted Boltzmann M ...

  10. C# ToString常用技巧总结

    ToString是在开发中最常用的操作,其作用是将其它类型以字符串类型表示.例如: int i=2;i.ToString() //”2″Object o = new Object();o.ToStri ...