0098. 验证二叉搜索树
最后更新于
这有帮助吗?
这有帮助吗?
/*
* @lc app=leetcode id=98 lang=javascript
*
* [98] Validate Binary Search Tree
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isValidBST = function (root) {
if (root === null) return true;
if (root.left === null && root.right === null) return true;
const stack = [root];
let cur = root;
let pre = null;
function insertAllLefts(cur) {
while (cur && cur.left) {
const l = cur.left;
stack.push(l);
cur = l;
}
}
insertAllLefts(cur);
while ((cur = stack.pop())) {
if (pre && cur.val <= pre.val) return false;
const r = cur.right;
if (r) {
stack.push(r);
insertAllLefts(r);
}
pre = cur;
}
return true;
};// 递归
class Solution {
public:
bool isValidBST(TreeNode* root) {
TreeNode* prev = nullptr;
return validateBstInorder(root, prev);
}
private:
bool validateBstInorder(TreeNode* root, TreeNode*& prev) {
if (root == nullptr) return true;
if (!validateBstInorder(root->left, prev)) return false;
if (prev != nullptr && prev->val >= root->val) return false;
prev = root;
return validateBstInorder(root->right, prev);
}
};
// 迭代
class Solution {
public:
bool isValidBST(TreeNode* root) {
auto s = vector<TreeNode*>();
TreeNode* prev = nullptr;
while (root != nullptr || !s.empty()) {
while (root != nullptr) {
s.push_back(root);
root = root->left;
}
root = s.back();
s.pop_back();
if (prev != nullptr && prev->val >= root->val) return false;
prev = root;
root = root->right;
}
return true;
}
};/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
Stack<Integer> stack = new Stack<> ();
TreeNode previous = null;
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
if (previous != null && root.val <= previous.val ) return false;
previous = root;
root = root.right;
}
return true;
}
}/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// 递归
class Solution {
public:
bool isValidBST(TreeNode* root) {
return helper(root, LONG_MIN, LONG_MAX);
}
private:
bool helper(const TreeNode* root, long min, long max) {
if (root == nullptr) return true;
if (root->val >= max || root->val <= min) return false;
return helper(root->left, min, root->val) && helper(root->right, root->val, max);
}
};
// 循环
class Solution {
public:
bool isValidBST(TreeNode* root) {
if (root == nullptr) return true;
auto ranges = queue<pair<long, long>>();
ranges.push(make_pair(LONG_MIN, LONG_MAX));
auto nodes = queue<const TreeNode*>();
nodes.push(root);
while (!nodes.empty()) {
auto sz = nodes.size();
for (auto i = 0; i < sz; ++i) {
auto range = ranges.front();
ranges.pop();
auto n = nodes.front();
nodes.pop();
if (n->val >= range.second || n->val <= range.first) {
return false;
}
if (n->left != nullptr) {
ranges.push(make_pair(range.first, n->val));
nodes.push(n->left);
}
if (n->right != nullptr) {
ranges.push(make_pair(n->val, range.second));
nodes.push(n->right);
}
}
}
return true;
}
};# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isValidBST(self, root: TreeNode, area: tuple=(-float('inf'), float('inf'))) -> bool:
"""思路如上面的分析,用Python表达会非常直白
:param root TreeNode 节点
:param area tuple 取值区间
"""
if root is None:
return True
is_valid_left = root.left is None or\
(root.left.val < root.val and area[0] < root.left.val < area[1])
is_valid_right = root.right is None or\
(root.right.val > root.val and area[0] < root.right.val < area[1])
# 左右节点都符合,说明本节点符合要求
is_valid = is_valid_left and is_valid_right
# 递归下去
return is_valid\
and self.isValidBST(root.left, (area[0], root.val))\
and self.isValidBST(root.right, (root.val, area[1]))/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
return helper(root, null, null);
}
private boolean helper(TreeNode root, Integer lower, Integer higher) {
if (root == null) return true;
if (lower != null && root.val <= lower) return false;
if (higher != null && root.val >= higher) return false;
if (!helper(root.left, lower, root.val)) return false;
if (!helper(root.right, root.val, higher)) return false;
return true;
}
}/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isValidBST = function (root) {
if (!root) return true;
return valid(root);
};
function valid(root, min = -Infinity, max = Infinity) {
if (!root) return true;
const val = root.val;
if (val <= min) return false;
if (val >= max) return false;
return valid(root.left, min, val) && valid(root.right, val, max);
}