Validating a Tree

public static boolean isValid( Node node ) {

 // A null node is valid 🙂
 if( node == null ) {
 return true;

 // Check that this is not a root node (which we don't need to validate)

 if( node.parent != null ) {
 // Determine if this is a left child or right child.
 if( node == node.parent.left ) {
 return <;
 } else {
 return >;

 // Continue with the rest of left and right subtrees.
 return isValid( node.left ) && isValid( node.right );

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


arpit tak

arpit tak

I like JAVA . I code. I chill. I blog.I eat. I sleep. I dream.

Personal Links

View Full Profile →


%d bloggers like this: