Ridiculously simple Ruby solution


  • 0
    Y
    def sorted_array_to_bst(arr)
      return nil if arr.nil? || arr.empty?
      
      mid = arr.length / 2
      n = TreeNode.new(arr[mid])
        n.left = sorted_array_to_bst(arr[0...mid])
        n.right = sorted_array_to_bst(arr[mid+1..-1])
      return n
    end
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.