0098. Validate Binary Search Tree
Last updated
Last updated
**Input:** root = [2,1,3]
**Output:** true**Input:** root = [5,1,4,null,null,3,6]
**Output:** false
**Explanation:** The root node's value is 5 but its right child's value is 4./**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private boolean firstNode = true; // first flag to handle corner test case, like it gives you MIN_VALUE;
private int pre = Integer.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
// Traversal, left first
if (!isValidBST(root.left)) return false;
// self
if (root.val <= pre && !firstNode) return false;
firstNode = false;
pre = root.val;
// right
if (!isValidBST(root.right)) return false;
return true;
}
}class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode pre = null;
TreeNode curr = root;
while (!stack.isEmpty() || curr != null) {
if (curr != null) {
stack.push(curr);
curr = curr.left;
} else {
curr = stack.pop();
if (pre != null && curr.val <= pre.val) return false;
pre = curr;
curr = curr.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) {
if (root == null) return true;
if (root.left == null && root.right == null) {
return true;
}
int left = max(root.left);
int right = min(root.right);
if (root.left == null && right <= root.val) return false;
if (root.right == null && left >= root.val) return false;
if (root.left != null && root.right != null) {
if (left >= root.val || right <= root.val) return false;
}
return isValidBST(root.left) && isValidBST(root.right);
}
private int min(TreeNode node) {
if (node == null) return Integer.MAX_VALUE;
if (node.left == null && node.right == null) {
return node.val;
}
int left = min(node.left);
int right = min(node.right);
int minChild = Math.min(left, right);
return Math.min(node.val, minChild);
}
private int max(TreeNode node) {
if (node == null) return Integer.MIN_VALUE;
if (node.left == null && node.right == null) {
return node.val;
}
int left = max(node.left);
int right = max(node.right);
int maxChild = Math.max(left, right);
return Math.max(node.val, maxChild);
}
}/**
* 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) {
// edge cases
if (root == null) return true;
return valid(root.left, Long.MIN_VALUE, root.val) && valid(root.right, root.val, Long.MAX_VALUE);
}
private boolean valid(TreeNode root, long low, long up) {
if (root == null) return true;
if (root.val <= low || root.val >= up) return false;
return valid(root.left, low, root.val) && valid(root.right, root.val, up);
}
}