I have written the following code to check if a tree is a Binary search tree. Please help me check the code:
Okay! The code is edited now. This simple solution was sugge
boolean isBST(TreeNode root, int max, int min) {
if (root == null) {
return true;
} else if (root.val >= max || root.val <= min) {
return false;
} else {
return isBST(root.left, root.val, min) && isBST(root.right, max, root.val);
}
}
an alternative way solving this problem.. similar with your code
A binary search tree has the following properties where the key for the left node must be <= the root node key and the right node key must be greater that the root.
So the problem we have is if the keys in the tree are not unique and a in order traversal was done we could get a situation of two in order traversals producing the same sequence, where 1 would be a valid bst and the other would not, this would happen if we had a tree where the left node = root(valid bst) and the right node = root(invalid not a bst).
To get around this we need to maintain a valid min/max range that the key being 'visited' must fall between, and we pass this range as we recurse to other nodes.
#include <limits>
int min = numeric_limits<int>::min();
int max = numeric_limits<int>::max();
The calling function will pass the above min and max values initially to isBst(...)
bool isBst(node* root, int min, int max)
{
//base case
if(root == NULL)
return true;
if(root->val <= max && root->val >= min)
{
bool b1 = isBst(root->left, min, root->val);
bool b2 = isBst(root->right, root->val, max);
if(!b1 || !b2)
return false;
return true;
}
return false;
}
right, another simple solution is do an inorder visit
java code here
boolean bst = isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
public boolean isBST(Node root, int min, int max) {
if(root == null)
return true;
return (root.data > min &&
root.data < max &&
isBST(root.left, min, root.data) &&
isBST(root.right, root.data, max));
}
public void inorder()
{
min=min(root);
//System.out.println(min);
inorder(root);
}
private int min(BSTNode r)
{
while (r.left != null)
{
r=r.left;
}
return r.data;
}
private void inorder(BSTNode r)
{
if (r != null)
{
inorder(r.getLeft());
System.out.println(r.getData());
if(min<=r.getData())
{
System.out.println(min+"<="+r.getData());
min=r.getData();
}
else
System.out.println("nananan");
//System.out.print(r.getData() +" ");
inorder(r.getRight());
return;
}
}
We do a depth-first walk through the tree, testing each node for validity as we go. A given node is valid if it's greater than all the ancestral nodes it's in the right sub-tree of and less than all the ancestral nodes it's in the left-subtree of. Instead of keeping track of each ancestor to check these inequalities, we just check the largest number it must be greater than (its lowerBound) and the smallest number it must be less than (its upperBound).
import java.util.Stack;
final int MIN_INT = Integer.MIN_VALUE;
final int MAX_INT = Integer.MAX_VALUE;
public class NodeBounds {
BinaryTreeNode node;
int lowerBound;
int upperBound;
public NodeBounds(BinaryTreeNode node, int lowerBound, int upperBound) {
this.node = node;
this.lowerBound = lowerBound;
this.upperBound = upperBound;
}
}
public boolean bstChecker(BinaryTreeNode root) {
// start at the root, with an arbitrarily low lower bound
// and an arbitrarily high upper bound
Stack<NodeBounds> stack = new Stack<NodeBounds>();
stack.push(new NodeBounds(root, MIN_INT, MAX_INT));
// depth-first traversal
while (!stack.empty()) {
NodeBounds nb = stack.pop();
BinaryTreeNode node = nb.node;
int lowerBound = nb.lowerBound;
int upperBound = nb.upperBound;
// if this node is invalid, we return false right away
if ((node.value < lowerBound) || (node.value > upperBound)) {
return false;
}
if (node.left != null) {
// this node must be less than the current node
stack.push(new NodeBounds(node.left, lowerBound, node.value));
}
if (node.right != null) {
// this node must be greater than the current node
stack.push(new NodeBounds(node.right, node.value, upperBound));
}
}
// if none of the nodes were invalid, return true
// (at this point we have checked all nodes)
return true;
}