Last active
December 12, 2015 03:29
-
-
Save charlespunk/4707635 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
Given a sorted(increasing order) array, write an algorithm to create a binary search tree with minimal height. |
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 Node creatBST(int[] array){ | |
return division(array, 0, array.length - 1); | |
} | |
public static Node division(int[] array, int begin, int end){ | |
if(begin < end) return null; | |
int mid = (begin + end) / 2; | |
Node thisNode = new Node(array[mid]); | |
thisNode.left = division(array, begin, mid - 1); | |
thisNode.right = division(array, mid + 1, end); | |
return thisNode; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment