> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/algo/binary-tree/convert-sorted-array-to-binary-search-tree.md).

# Convert Sorted Array to Binary Search Tree

{% embed url="<https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/>" %}

## Solution 1

```go
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func sortedArrayToBST(nums []int) *TreeNode {
    if len(nums) == 0 {
        return nil
    }
    n := TreeNode{
        Val: nums[len(nums)/2],
        Left: sortedArrayToBST(nums[:len(nums)/2]),
        Right: sortedArrayToBST(nums[len(nums)/2+1:]),
    }
    return &n
}
```
