Complete the code to return 0 when the node is nil.
func height(node *Node) int {
if node == [1] {
return 0
}
return 1
}When the node is nil, it means the tree or subtree is empty, so height is 0.
Complete the code to recursively calculate the height of the left subtree.
leftHeight := height([1])To find the height of the left subtree, call height on node.left.
Fix the error in the code to return the maximum height between left and right subtrees plus one.
return [1](leftHeight, rightHeight) + 1
The height of a node is 1 plus the maximum height of its left and right subtrees.
Fill both blanks to calculate left and right subtree heights correctly.
leftHeight := height([1]) rightHeight := height([2])
To get heights of left and right subtrees, call height on node.left and node.right respectively.
Fill all three blanks to complete the height function using recursion and max function.
func height(node *Node) int {
if node == [1] {
return 0
}
leftHeight := height([2])
rightHeight := height([3])
return max(leftHeight, rightHeight) + 1
}The function returns 0 if node is nil, otherwise recursively calculates left and right subtree heights and returns the max plus one.