Given the root of a binary tree, determine if it is a valid binary search tree (BST).
A valid 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:** root = [2,1,3]
**Output:** true
Example 2:
**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.
Constraints:
The number of nodes in the tree is in the range [1, 104].
-231 <= Node.val <= 231 - 1
ac1: inorder traversal
BST, inorder traversal is transcendent.
Traversal: consider three part: left,right and itself, it's about order.
/**
* 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;
}
}
ac2: iterative, same as traversal
in this case, iteration is easier to understand.
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;
}
}
First answer, so ugly, must be wrong.
/**
* 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);
}
}
ac3: recursive
becareful about the boudary, especially Integer.MAX_VALUEInteger.MIN_VALUE
2/18/2018
/**
* 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);
}
}