I've been a zone leader with DZone since 2008, and I'm crazy about community. Every day I get to work with the best that JavaScript, HTML5, Android and iOS has to offer, creating apps that truly make at difference, as principal front-end architect at Avego. James is a DZone Zone Leader and has posted 639 posts at DZone. You can read more from them at their website. View Full User Profile

Thursday Code Puzzler: Verification of a Binary Search Tree

  • submit to reddit

Thursday is code puzzler day here at DZone. The idea is simple: solve the coding problem as efficiently as you can, in any language or framework that you find suitable.

Note: Even though there really is nothing stopping you from finding a solution to this on the internet, try to keep honest, and come up with your own answer.  It's all about the participation!

Do you have code puzzlers that you'd like to share with the DZone community?  If so, please submit here. 

Verify if the tree is a binary search tree

Given a tree structure, provide a method that will verify whether or not it is actually a binary search tree. The Tree consists of a number of Node objects, each of which has a value and a reference to a right and left Node object.

Note that a BST is different to a standard binary tree because of the following properties : 

  • The leftsubtreeof 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.
  • There must be no duplicate nodes.

For more on Binary Search Trees, check this article  on Wikipedia.

Catch up on all our previous puzzlers here.


Vijay Nathani replied on Thu, 2013/01/03 - 5:22am

 Groovy code

@groovy.transform.Immutable class Node { int n; Node left, right; }
class BinaryTree {
	def nodes = new HashSet()
	def count = 0
	def verifyTreeStructure(Node root) {
		if (!root) return true
		count++; nodes.add(root.n)
		return (!root.left || root.left.n < root.n) && (!root.right || root.right.n > root.n) && verifyTreeStructure(root.left) && verifyTreeStructure(root.right)
	static def verify(Node root) {
		def c = new BinaryTree()
		return c.verifyTreeStructure(root) && c.nodes.size() == c.count
assert BinaryTree.verify(new Node(2,new Node(1,null,null),new Node(3,null,null)))

Erik Colban replied on Thu, 2013/01/03 - 8:32pm

	boolean verifyBST(Node root) {
		return verifyBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);

	boolean verifyBST(Node node, int min, int max) {
		if (node == null)
			return true;
		if (node.getKey() == min && node.getLeft() != null)
			return false;
		if (node.getKey() == max && node.getRight() != null)
			return false;
		return min <= node.getKey() && node.getKey() <= max
				&& verifyBST(node.getLeft(), min, node.getKey() - 1)
				&& verifyBST(node.getRight(), node.getKey() + 1, max);


Vijay Nathani replied on Fri, 2013/01/04 - 1:58am in response to: Erik Colban

This is a better solution than mine because my previous solution did not make certain checks. The solution in groovy is 

@groovy.transform.Immutable class Node { 
	int n; Node left, right; 
	def boolean verify() {
		return verifyBST(Integer.MIN_VALUE, Integer.MAX_VALUE)
	private def boolean verifyBST(long min, long max) {
		if (n < min || n > max) return false
		return (!left || left.verifyBST(min,n-1)) && (!right || right.verifyBST(n+1,max))
assert new Node(2,new Node(1,null,null),new Node(3,null,null)).verify()

Erik Colban replied on Fri, 2013/01/04 - 4:09am


Are you assuming that the key is an int or a long? If the key is an int, then the arguments of verifyBST could be int's. The problem then is that if min == Integer.MIN_VALUE and n == min, then n - 1 == Integer.MAX_VALUE.  I guess that is the reason you used long's.

I suggest the following change:

private def boolean verifyBST(int min, int max) {
		return min <= n && n <= max
			&& (!left || min < n && left.verifyBST(min,n-1)) 
			&& (!right || n < max && right.verifyBST(n+1,max))

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.