[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 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:
- Input:
- 2
- / \
- 1 3
- Output: true
Example 2:
- 5
- / \
- 1 4
- / \
- 3 6
- Output: false
- Explanation: The input is: [5,1,4,null,null,3,6]. The root node's value
- is 5 but its right child's value is 4.
这道验证二叉搜索树有很多种解法,可以利用它本身的性质来做,即左<根<右,也可以通过利用中序遍历结果为有序数列来做,下面我们先来看最简单的一种,就是利用其本身性质来做,初始化时带入系统最大值和最小值,在递归过程中换成它们自己的节点值,用long代替int就是为了包括int的边界条件,代码如下:
C++ 解法一:
- // Recursion without inorder traversal
- class Solution {
- public:
- bool isValidBST(TreeNode* root) {
- return isValidBST(root, LONG_MIN, LONG_MAX);
- }
- bool isValidBST(TreeNode* root, long mn, long mx) {
- if (!root) return true;
- if (root->val <= mn || root->val >= mx) return false;
- return isValidBST(root->left, mn, root->val) && isValidBST(root->right, root->val, mx);
- }
- };
Java 解法一:
- public class Solution {
- public boolean isValidBST(TreeNode root) {
- if (root == null) return true;
- return valid(root, Long.MIN_VALUE, Long.MAX_VALUE);
- }
- public boolean valid(TreeNode root, long low, long high) {
- if (root == null) return true;
- if (root.val <= low || root.val >= high) return false;
- return valid(root.left, low, root.val) && valid(root.right, root.val, high);
- }
- }
这题实际上简化了难度,因为有的时候题目中的二叉搜索树会定义为左<=根<右,而这道题设定为一般情况左<根<右,那么就可以用中序遍历来做。因为如果不去掉左=根这个条件的话,那么下边两个数用中序遍历无法区分:
20 20
/ \
20 20
它们的中序遍历结果都一样,但是左边的是 BST,右边的不是 BST。去掉等号的条件则相当于去掉了这种限制条件。下面来看使用中序遍历来做,这种方法思路很直接,通过中序遍历将所有的节点值存到一个数组里,然后再来判断这个数组是不是有序的,代码如下:
C++ 解法二:
- // Recursion
- class Solution {
- public:
- bool isValidBST(TreeNode* root) {
- if (!root) return true;
- vector<int> vals;
- inorder(root, vals);
- for (int i = ; i < vals.size() - ; ++i) {
- if (vals[i] >= vals[i + ]) return false;
- }
- return true;
- }
- void inorder(TreeNode* root, vector<int>& vals) {
- if (!root) return;
- inorder(root->left, vals);
- vals.push_back(root->val);
- inorder(root->right, vals);
- }
- };
Java 解法二:
- public class Solution {
- public boolean isValidBST(TreeNode root) {
- List<Integer> list = new ArrayList<Integer>();
- inorder(root, list);
- for (int i = 0; i < list.size() - 1; ++i) {
- if (list.get(i) >= list.get(i + 1)) return false;
- }
- return true;
- }
- public void inorder(TreeNode node, List<Integer> list) {
- if (node == null) return;
- inorder(node.left, list);
- list.add(node.val);
- inorder(node.right, list);
- }
- }
下面这种解法跟上面那个很类似,都是用递归的中序遍历,但不同之处是不将遍历结果存入一个数组遍历完成再比较,而是每当遍历到一个新节点时和其上一个节点比较,如果不大于上一个节点那么则返回 false,全部遍历完成后返回 true。代码如下:
C++ 解法三:
- class Solution {
- public:
- bool isValidBST(TreeNode* root) {
- TreeNode *pre = NULL;
- return inorder(root, pre);
- }
- bool inorder(TreeNode* node, TreeNode*& pre) {
- if (!node) return true;
- bool res = inorder(node->left, pre);
- if (!res) return false;
- if (pre) {
- if (node->val <= pre->val) return false;
- }
- pre = node;
- return inorder(node->right, pre);
- }
- };
当然这道题也可以用非递归来做,需要用到栈,因为中序遍历可以非递归来实现,所以只要在其上面稍加改动便可,代码如下:
C++ 解法四:
- class Solution {
- public:
- bool isValidBST(TreeNode* root) {
- stack<TreeNode*> s;
- TreeNode *p = root, *pre = NULL;
- while (p || !s.empty()) {
- while (p) {
- s.push(p);
- p = p->left;
- }
- p = s.top(); s.pop();
- if (pre && p->val <= pre->val) return false;
- pre = p;
- p = p->right;
- }
- return true;
- }
- };
Java 解法四:
- public class Solution {
- public boolean isValidBST(TreeNode root) {
- Stack<TreeNode> s = new Stack<TreeNode>();
- TreeNode p = root, pre = null;
- while (p != null || !s.empty()) {
- while (p != null) {
- s.push(p);
- p = p.left;
- }
- p = s.pop();
- if (pre != null && p.val <= pre.val) return false;
- pre = p;
- p = p.right;
- }
- return true;
- }
- }
最后还有一种方法,由于中序遍历还有非递归且无栈的实现方法,称之为 Morris 遍历,可以参考博主之前的博客 Binary Tree Inorder Traversal,这种实现方法虽然写起来比递归版本要复杂的多,但是好处在于是 O(1) 空间复杂度,参见代码如下:
C++ 解法五:
- class Solution {
- public:
- bool isValidBST(TreeNode *root) {
- if (!root) return true;
- TreeNode *cur = root, *pre, *parent = NULL;
- bool res = true;
- while (cur) {
- if (!cur->left) {
- if (parent && parent->val >= cur->val) res = false;
- parent = cur;
- cur = cur->right;
- } else {
- pre = cur->left;
- while (pre->right && pre->right != cur) pre = pre->right;
- if (!pre->right) {
- pre->right = cur;
- cur = cur->left;
- } else {
- pre->right = NULL;
- if (parent->val >= cur->val) res = false;
- parent = cur;
- cur = cur->right;
- }
- }
- }
- return res;
- }
- };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/98
类似题目:
Find Mode in Binary Search Tree
参考资料:
https://leetcode.com/problems/validate-binary-search-tree/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- 098 Validate Binary Search Tree 验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树.一个二叉搜索树有如下定义: 左子树只包含小于当前节点的数. 右子树只包含大于当前节点的数. 所有子树自身必须也是二叉搜索树.示例 1 ...
- Leetcode98. Validate Binary Search Tree验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...
- [LeetCode98]98. Validate Binary Search Tree判断二叉搜索树
判断二叉搜索树的方法是: 中序遍历形成递增序列 //全局变量记录中序遍历产生的序列,因为要递归,所以要用全局变量 List<Integer> list = new ArrayList< ...
- [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 ...
- [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 ...
- [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
随机推荐
- LeetCode 234:回文链表 Palindrome Linked List
请判断一个链表是否为回文链表. Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: ...
- Elasticsearch搜索调优权威指南 (1/3)
本文首发于 vivo互联网技术 微信公众号 https://mp.weixin.qq.com/s/qwkZKLb_ghmlwrqMkqlb7Q英文原文:https://qbox.io/blog/ela ...
- kali渗透综合靶机(十八)--FourAndSix2靶机
kali渗透综合靶机(十八)--FourAndSix2靶机 靶机下载地址:https://download.vulnhub.com/fourandsix/FourAndSix2.ova 一.主机发现 ...
- Winform中设置ZedGraph的曲线为散点图
场景 Winform中设置ZedGraph的曲线符号Symbol以及对应关系: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- Effective Python 编写高质量Python代码的59个有效方法
Effective Python 编写高质量Python代码的59个有效方法
- 【设计模式】Bridge
前言 Bridge设计模式,将一个复杂类分成可以单独开发的部分.分成的两个部分,abstraction,implementation.字面上是抽象和实现,但不同于抽象方法及其实现.下面摘录Wiki的两 ...
- 【AI测试】也许这有你想知道的人工智能 (AI) 测试--第二篇
概述此为人工智能 (AI) 测试第二篇 第一篇主要介绍了 人工智能测试.测试什么.测试数据等.第二篇主要介绍测试用例和测试报告.之后的文章可能具体介绍如何开展各项测试,以及具体项目举例如何测试.测试用 ...
- 剑指:和为S的连续正数序列
题目描述 输入一个正数 s,打印出所有和为 s 的连续正数序列(至少含有两个数). 例如输入 15,由于 1+2+3+4+5=4+5+6=7+8=15,所以结果打印出 3 个连续序列 1-5.4-6 ...
- php session的理解【转】
目录 1.什么是session? 2.Session常见函数及用法? ● 如何删除session? ● SESSION安全: Session跨页传递问题: 1.什么是session? Sessio ...
- 6、Routing
Routing In the previous tutorial we built a simple logging system. We were able to broadcast log mes ...