Last active
August 29, 2015 14:13
-
-
Save arjunrao87/5bb20a5210e55a6d3167 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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