Created
July 11, 2014 19:50
-
-
Save jingz8804/81e1e22299900497f3af to your computer and use it in GitHub Desktop.
#ctci
This file contains 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 class BSTWithMinHeight{ | |
private class Node{ | |
int val; | |
Node left; | |
Node right; | |
public Node(int val){ | |
this.val = val; | |
} | |
} | |
public Node createBST(int[] data){ | |
if(data == null) return null; | |
return createBST(data, 0, data.length - 1); | |
} | |
private Node createBST(int[] data, int low, int high){ | |
if(low > high) return null; | |
if(low == high) return new Node(data[low]); | |
int mid = (low + high)/2; | |
Node root = new Node(data[mid]); | |
root.left = createBST(data, low, mid-1); | |
root.right = createBST(data, mid+1, high); | |
return root; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment