Complete the code to create the root node of a tree.
root := &Node{Value: 10, [1]: nil, Left: nil, Right: nil}The root node has no parent, so its Parent field is nil.
Complete the code to check if a node is a leaf (no children).
func isLeaf(node *Node) bool {
return node.Left == nil && node.[1] == nil
}A leaf node has no left or right children, so both Left and Right are nil.
Fix the error in the code to calculate the height of a node.
func height(node *Node) int {
if node == nil {
return -1
}
leftHeight := height(node.Left)
rightHeight := height(node.[1])
if leftHeight > rightHeight {
return leftHeight + 1
}
return rightHeight + 1
}To find height, we check both Left and Right children recursively.
Fill both blanks to compute the depth of a node in the tree.
func depth(node *Node) int {
if node.[1] == nil {
return 0
}
return 1 + depth(node.[2])
}Depth is the number of edges from the node to the root, so we move up using Parent.
Fill all three blanks to create a map of node levels in the tree.
func assignLevels(node *Node, level int, levels map[*Node]int) {
if node == nil {
return
}
levels[node] = [1]
assignLevels(node.[2], level [3], levels)
assignLevels(node.Right, level+1, levels)
}We assign the current level to the node, then recurse on children with level + 1.
