Skip to content

Instantly share code, notes, and snippets.

@wszdwp
Created November 8, 2014 17:54
Show Gist options
  • Select an option

  • Save wszdwp/7d9a84bd2bc79e36bc48 to your computer and use it in GitHub Desktop.

Select an option

Save wszdwp/7d9a84bd2bc79e36bc48 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) {
return sortedArrayToBST(num, 0, num.length-1);
}
public TreeNode sortedArrayToBST(int[] num, int lo, int hi) {
TreeNode root = null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment