题目:
题解:
var globalHead *ListNodefunc sortedListToBST(head *ListNode) *TreeNode {globalHead = headlength := getLength(head)return buildTree(0, length - 1)
}func getLength(head *ListNode) int {ret := 0for ; head != nil; head = head.Next {ret++}return ret
}func buildTree(left, right int) *TreeNode {if left > right {return nil}mid := (left + right + 1) / 2root := &TreeNode{}root.Left = buildTree(left, mid - 1)root.Val = globalHead.ValglobalHead = globalHead.Nextroot.Right = buildTree(mid + 1, right)return root
}