# Tree - 108. Convert Sorted Array to Binary Search Tree Easy

108. Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example:

```Given the sorted array: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

0
/ \
-3   9
/   /
-10  5```

go：

```/**

* Definition for a binary tree node.
* type TreeNode struct {
*     Val int
*     Left *TreeNode
*     Right *TreeNode
* }
*/
func sortedArrayToBST(nums []int) *TreeNode {
if nums == nil || len(nums) == 0 {
return nil
}

return recursion(nums, 0, len(nums))
}

func recursion(nums []int, low int, high int) *TreeNode {
if low >= high { // leaf
return nil
}

mid := (high-low)/2 + low

return &TreeNode{
Val:   nums[mid],
Left:  recursion(nums, low, mid),
Right: recursion(nums, mid+1, high)}
}```

0 条评论

• ### Array - 16. 3Sum Closest

Given an array nums of n integers and an integer target, find three integers in ...

• ### Array - 15. 3Sum

Given an array nums of n integers, are there elements a, b, c in nums such that ...

• ### Array - 508. Wiggle Sort

Given an unsorted array nums, reorder it in-place such that

• ### LeetCode - 存在重复元素

原题地址：https://leetcode-cn.com/problems/contains-duplicate/

• ### 单调栈-LeetCode 739、287（单调栈，桶计数）

根据每日 气温 列表，请重新生成一个列表，对应位置的输入是你需要再等待多久温度才会升高超过该日的天数。如果之后都不会升高，请在该位置用 0 来代替。 例如，给定...

• ### LWC 54：698. Partition to K Equal Sum Subsets

LWC 54：698. Partition to K Equal Sum Subsets 传送门：698. Partition to K Equal Sum S...

• ### LeetCode第53题

Given an integer array nums, find the contiguous subarray (containing at least o...

• ### LeetCode 666. 路径和 IV（树的遍历）

给定一个包含三位整数的升序数组，表示一棵深度小于 5 的二叉树， 请你返回从根到所有叶子结点的路径之和。

• ### LeetCode第七天

==数组 Medium== 40.（162）Find Peak Element ? JAVA //斜率思想，二分法 class Solution { p...

• ### 219. Contains Duplicate II

Given an array of integers and an integer k, find out whether there are two dist...