Convert Sorted Array to Binary Search Tree
Solution in go
func sortedArrayToBST(nums []int) *TreeNode {
    return getTreeNode(nums, 0, len(nums) - 1)
}

func getTreeNode(nums []int, left, right int) *TreeNode {
    if left > right {
        return nil
    }

    mid := left + ((right - left) / 2)
    root := &TreeNode{
        Val: nums[mid],
    }

    root.Left = getTreeNode(nums, left, mid - 1)
    root.Right = getTreeNode(nums, mid + 1, right)

    return root
}
    TheDeveloperCafe © 2022-2024