## 0x00 题目

​​`​高度差​`​​ 的绝对值不超过 ​`​1​`

## 0x02 解法

`public class TreeNode {    public var val: Int    public var left: TreeNode?    public var right: TreeNode?    public init() { self.val = 0; self.left = nil; self.right = nil; }    public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }    public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {        self.val = val        self.left = left        self.right = right    }}`

`func sortedListToBST(_ head: ListNode?) -> TreeNode? {    if head == nil { return nil }    // 将  链表 转换成 数组    var tmp = head    var arr: [Int] = []    while tmp != nil {        arr.append(tmp!.val)        tmp = tmp?.next    }    // 有序数组 转换为 二叉搜索树    return toBST(arr, 0, arr.count - 1)}func toBST(_ nums: [Int], _ left: Int, _ right: Int) -> TreeNode? {    if left > right { return nil }    let mid = (left + right) / 2    let val = nums[mid]        // 根节点    let node = TreeNode(val)        // 左子树    node.left = toBST(nums, left, mid - 1)        // 右子树    node.right = toBST(nums, mid + 1, right)    return node}`