You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: Solution.go
    path of file: ./repos/awesome-golang-algorithm/leetcode/1301-1400/1382.Balance-a-Binary-Search-Tree
    
    the code of the file until where you have to start completion: package Solution

type TreeNode struct {
	Val         int
	Left, Right *TreeNode
}

func Solution(root *TreeNode) *TreeNode {
	var targe
    
func rebuildBST(root *TreeNode, val int, depth map[*TreeNode]int) *TreeNode {
	newNode := &TreeNode{
		Val: val,
	}
	depth[newNode] = 1

	if root == nil {
		return newNode
	}
	if root.Val < val {
		root.Right = rebuildBST(root.Right, val, depth)
	} else if root.Val > val {
		root.Left = rebuildBST(root.Left, val, depth)
	} else {
		return root
	}

	hl := depth[root.Left]
	hr := depth[root.Right]
	rootHeight := hl + 1
	if hr+1 > rootHeight {
		rootHeight = hr + 1
	}
	depth[root] = rootHeight

	diff := hl - hr
	if diff > 1 && val < root.Left.Val {
		return rightRotate(root, depth)
	}
	if diff > 1 && val > root.Left.Val {
		root.Left = leftRotate(root.Left, depth)
		return rightRotate(root, depth)
	}
	if diff < -1 && val > root.Right.Val {
		return leftRotate(root, depth)
	}
	if diff < -1 && val < root.Right.Val {
		root.Right = rightRotate(root.Right, depth)
		return leftRotate(root, depth)
	}
	return root
}