Skip to content

Instantly share code, notes, and snippets.

@zac-xin
Created December 9, 2012 16:34
Show Gist options
  • Save zac-xin/4245928 to your computer and use it in GitHub Desktop.
Save zac-xin/4245928 to your computer and use it in GitHub Desktop.
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode sortedArrayToBST(int[] num) {
// Start typing your Java solution below
// DO NOT write main() function
return convert(num, 0, num.length - 1);
}
public TreeNode convert(int a[], int low, int high){
if(low <= high){
int mid = (low + high) / 2;
TreeNode root = new TreeNode(a[mid]);
TreeNode left = convert(a, low, mid -1);
TreeNode right = convert(a, mid + 1, high);
root.left = left;
root.right = right;
return root;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment