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.

Example 1:

  1. Input:
  2. 2
  3. / \
  4. 1 3
  5. Output: true

Example 2:

  1. 5
  2. / \
  3. 1 4
  4.   / \
  5.   3 6
  6. Output: false
  7. Explanation: The input is: [5,1,4,null,null,3,6]. The root node's value
  8.   is 5 but its right child's value is 4.

这道验证二叉搜索树有很多种解法,可以利用它本身的性质来做,即左<根<右,也可以通过利用中序遍历结果为有序数列来做,下面我们先来看最简单的一种,就是利用其本身性质来做,初始化时带入系统最大值和最小值,在递归过程中换成它们自己的节点值,用long代替int就是为了包括int的边界条件,代码如下:

C++ 解法一:

  1. // Recursion without inorder traversal
  2. class Solution {
  3. public:
  4. bool isValidBST(TreeNode* root) {
  5. return isValidBST(root, LONG_MIN, LONG_MAX);
  6. }
  7. bool isValidBST(TreeNode* root, long mn, long mx) {
  8. if (!root) return true;
  9. if (root->val <= mn || root->val >= mx) return false;
  10. return isValidBST(root->left, mn, root->val) && isValidBST(root->right, root->val, mx);
  11. }
  12. };

Java 解法一:

  1. public class Solution {
  2. public boolean isValidBST(TreeNode root) {
  3. if (root == null) return true;
  4. return valid(root, Long.MIN_VALUE, Long.MAX_VALUE);
  5. }
  6. public boolean valid(TreeNode root, long low, long high) {
  7. if (root == null) return true;
  8. if (root.val <= low || root.val >= high) return false;
  9. return valid(root.left, low, root.val) && valid(root.right, root.val, high);
  10. }
  11. }

这题实际上简化了难度,因为有的时候题目中的二叉搜索树会定义为左<=根<右,而这道题设定为一般情况左<根<右,那么就可以用中序遍历来做。因为如果不去掉左=根这个条件的话,那么下边两个数用中序遍历无法区分:

20       20
   /           \
 20           20

它们的中序遍历结果都一样,但是左边的是 BST,右边的不是 BST。去掉等号的条件则相当于去掉了这种限制条件。下面来看使用中序遍历来做,这种方法思路很直接,通过中序遍历将所有的节点值存到一个数组里,然后再来判断这个数组是不是有序的,代码如下:

C++ 解法二:

  1. // Recursion
  2. class Solution {
  3. public:
  4. bool isValidBST(TreeNode* root) {
  5. if (!root) return true;
  6. vector<int> vals;
  7. inorder(root, vals);
  8. for (int i = ; i < vals.size() - ; ++i) {
  9. if (vals[i] >= vals[i + ]) return false;
  10. }
  11. return true;
  12. }
  13. void inorder(TreeNode* root, vector<int>& vals) {
  14. if (!root) return;
  15. inorder(root->left, vals);
  16. vals.push_back(root->val);
  17. inorder(root->right, vals);
  18. }
  19. };

Java 解法二:

  1. public class Solution {
  2. public boolean isValidBST(TreeNode root) {
  3. List<Integer> list = new ArrayList<Integer>();
  4. inorder(root, list);
  5. for (int i = 0; i < list.size() - 1; ++i) {
  6. if (list.get(i) >= list.get(i + 1)) return false;
  7. }
  8. return true;
  9. }
  10. public void inorder(TreeNode node, List<Integer> list) {
  11. if (node == null) return;
  12. inorder(node.left, list);
  13. list.add(node.val);
  14. inorder(node.right, list);
  15. }
  16. }

下面这种解法跟上面那个很类似,都是用递归的中序遍历,但不同之处是不将遍历结果存入一个数组遍历完成再比较,而是每当遍历到一个新节点时和其上一个节点比较,如果不大于上一个节点那么则返回 false,全部遍历完成后返回 true。代码如下:

C++ 解法三:

  1. class Solution {
  2. public:
  3. bool isValidBST(TreeNode* root) {
  4. TreeNode *pre = NULL;
  5. return inorder(root, pre);
  6. }
  7. bool inorder(TreeNode* node, TreeNode*& pre) {
  8. if (!node) return true;
  9. bool res = inorder(node->left, pre);
  10. if (!res) return false;
  11. if (pre) {
  12. if (node->val <= pre->val) return false;
  13. }
  14. pre = node;
  15. return inorder(node->right, pre);
  16. }
  17. };

当然这道题也可以用非递归来做,需要用到栈,因为中序遍历可以非递归来实现,所以只要在其上面稍加改动便可,代码如下:

C++ 解法四:

  1. class Solution {
  2. public:
  3. bool isValidBST(TreeNode* root) {
  4. stack<TreeNode*> s;
  5. TreeNode *p = root, *pre = NULL;
  6. while (p || !s.empty()) {
  7. while (p) {
  8. s.push(p);
  9. p = p->left;
  10. }
  11. p = s.top(); s.pop();
  12. if (pre && p->val <= pre->val) return false;
  13. pre = p;
  14. p = p->right;
  15. }
  16. return true;
  17. }
  18. };

Java 解法四:

  1. public class Solution {
  2. public boolean isValidBST(TreeNode root) {
  3. Stack<TreeNode> s = new Stack<TreeNode>();
  4. TreeNode p = root, pre = null;
  5. while (p != null || !s.empty()) {
  6. while (p != null) {
  7. s.push(p);
  8. p = p.left;
  9. }
  10. p = s.pop();
  11. if (pre != null && p.val <= pre.val) return false;
  12. pre = p;
  13. p = p.right;
  14. }
  15. return true;
  16. }
  17. }

最后还有一种方法,由于中序遍历还有非递归且无栈的实现方法,称之为 Morris 遍历,可以参考博主之前的博客 Binary Tree Inorder Traversal,这种实现方法虽然写起来比递归版本要复杂的多,但是好处在于是 O(1) 空间复杂度,参见代码如下:

C++ 解法五:

  1. class Solution {
  2. public:
  3. bool isValidBST(TreeNode *root) {
  4. if (!root) return true;
  5. TreeNode *cur = root, *pre, *parent = NULL;
  6. bool res = true;
  7. while (cur) {
  8. if (!cur->left) {
  9. if (parent && parent->val >= cur->val) res = false;
  10. parent = cur;
  11. cur = cur->right;
  12. } else {
  13. pre = cur->left;
  14. while (pre->right && pre->right != cur) pre = pre->right;
  15. if (!pre->right) {
  16. pre->right = cur;
  17. cur = cur->left;
  18. } else {
  19. pre->right = NULL;
  20. if (parent->val >= cur->val) res = false;
  21. parent = cur;
  22. cur = cur->right;
  23. }
  24. }
  25. }
  26. return res;
  27. }
  28. };

Github 同步地址:

https://github.com/grandyang/leetcode/issues/98

类似题目:

Binary Tree Inorder Traversal

Find Mode in Binary Search Tree

参考资料:

https://leetcode.com/problems/validate-binary-search-tree/

https://leetcode.com/problems/validate-binary-search-tree/discuss/32101/My-java-inorder-iteration-solution

https://leetcode.com/problems/validate-binary-search-tree/discuss/32109/My-simple-Java-solution-in-3-lines

https://leetcode.com/problems/validate-binary-search-tree/discuss/32112/Learn-one-iterative-inorder-traversal-apply-it-to-multiple-tree-questions-(Java-Solution)

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树的更多相关文章

  1. [leetcode]98. Validate Binary Search Tree验证二叉搜索树

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

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

    4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...

  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. 098 Validate Binary Search Tree 验证二叉搜索树

    给定一个二叉树,判断其是否是一个有效的二叉搜索树.一个二叉搜索树有如下定义:    左子树只包含小于当前节点的数.    右子树只包含大于当前节点的数.    所有子树自身必须也是二叉搜索树.示例 1 ...

  5. Leetcode98. Validate Binary Search Tree验证二叉搜索树

    给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...

  6. [LeetCode98]98. Validate Binary Search Tree判断二叉搜索树

    判断二叉搜索树的方法是: 中序遍历形成递增序列 //全局变量记录中序遍历产生的序列,因为要递归,所以要用全局变量 List<Integer> list = new ArrayList< ...

  7. [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 ...

  8. [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 ...

  9. [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

随机推荐

  1. LeetCode 234:回文链表 Palindrome Linked List

    ​ 请判断一个链表是否为回文链表. Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: ...

  2. Elasticsearch搜索调优权威指南 (1/3)

    本文首发于 vivo互联网技术 微信公众号 https://mp.weixin.qq.com/s/qwkZKLb_ghmlwrqMkqlb7Q英文原文:https://qbox.io/blog/ela ...

  3. kali渗透综合靶机(十八)--FourAndSix2靶机

    kali渗透综合靶机(十八)--FourAndSix2靶机 靶机下载地址:https://download.vulnhub.com/fourandsix/FourAndSix2.ova 一.主机发现 ...

  4. Winform中设置ZedGraph的曲线为散点图

    场景 Winform中设置ZedGraph的曲线符号Symbol以及对应关系: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  5. Effective Python 编写高质量Python代码的59个有效方法

    Effective Python 编写高质量Python代码的59个有效方法

  6. 【设计模式】Bridge

    前言 Bridge设计模式,将一个复杂类分成可以单独开发的部分.分成的两个部分,abstraction,implementation.字面上是抽象和实现,但不同于抽象方法及其实现.下面摘录Wiki的两 ...

  7. 【AI测试】也许这有你想知道的人工智能 (AI) 测试--第二篇

    概述此为人工智能 (AI) 测试第二篇 第一篇主要介绍了 人工智能测试.测试什么.测试数据等.第二篇主要介绍测试用例和测试报告.之后的文章可能具体介绍如何开展各项测试,以及具体项目举例如何测试.测试用 ...

  8. 剑指:和为S的连续正数序列

    题目描述 输入一个正数 s,打印出所有和为 s 的连续正数序列(至少含有两个数). 例如输入 15,由于 1+2+3+4+5=4+5+6=7+8=15,所以结果打印出 3 个连续序列 1-5.4-6 ...

  9. php session的理解【转】

    目录 1.什么是session? 2.Session常见函数及用法? ● 如何删除session? ● SESSION安全: Session跨页传递问题: 1.什么是session?   Sessio ...

  10. 6、Routing

    Routing In the previous tutorial we built a simple logging system. We were able to broadcast log mes ...