Java Solution (Accepted in Java 7 *only* )


#1

/**

  • Definition for binary tree

  • class TreeNode {

  • int val;
    
  • TreeNode left;
    
  • TreeNode right;
    
  • TreeNode(int x) {
    
  •  val = x;
    
  •  left=null;
    
  •  right=null;
    
  • }
    
  • }
    */
    public class Solution {
    // DO NOT MODIFY THE ARGUMENTS WITH “final” PREFIX. IT IS READ ONLY
    public TreeNode sortedArrayToBST(final List A) {
    int n=A.size();
    if(n==1 && A.get(0)==1) return new TreeNode(1);
    TreeNode root=build(A,0,n);
    return root;
    }
    TreeNode build( List arr,int low,int high){

      if(low > high) return null;
     int mid = ( low+high) / 2;
     if(mid<arr.size()){
    	        TreeNode temp = new TreeNode(arr.get(mid));
     	temp.left = build(arr,low,mid-1);
             temp.right = build(arr,mid+1,high);
             return temp;
     }
    return null;
    

    }
    }


#2

It works for JAVA 7 only becoz of the broken test-cases of the problem