Skip to content

Instantly share code, notes, and snippets.

@arjunrao87
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save arjunrao87/5bb20a5210e55a6d3167 to your computer and use it in GitHub Desktop.

Select an option

Save arjunrao87/5bb20a5210e55a6d3167 to your computer and use it in GitHub Desktop.
public static LNode _head;
public BSTNode convert( LNode head ){
_head = head;
int len = count( head );
if( len == 0 ){
return null;
}
return convert( 0, len - 1 );
}
public int count( LNode node ){
int count = 0;
while( node != null ){
node = node.next;
count++;
}
return count;
}
public BSTNode convert( int start, int stop ){
if( start >= stop ){
return null;
}
int mid = ( start + stop ) / 2;
BSTNode left = convert( start, mid - 1 );
BSTNode root = new BSTNode( _head.data );
_head = _head.next;
BSTNode right = convert( mid + 1, stop );
root.left = left;
root.right = right;
return root;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment