Complete the code to call the postorder traversal function recursively on the left child.
func postorder(node *Node) {
if node == nil {
return
}
postorder(node.[1])
// Continue traversal
}In postorder traversal, we first visit the left child, so we call the function on node.Left.
Complete the code to call the postorder traversal function recursively on the right child.
func postorder(node *Node) {
if node == nil {
return
}
postorder(node.Left)
postorder(node.[1])
// Continue traversal
}After visiting the left child, postorder traversal visits the right child, so we call the function on node.Right.
Fix the error in the code to print the node's value after visiting its children.
func postorder(node *Node) {
if node == nil {
return
}
postorder(node.Left)
postorder(node.Right)
fmt.Println(node.[1])
}In postorder traversal, after visiting left and right children, we print the node's Value.
Fill both blanks to complete the postorder traversal function that returns a slice of node values.
func postorderValues(node *Node) []int {
if node == nil {
return []int{}
}
left := postorderValues(node.[1])
right := postorderValues(node.[2])
return append(append(left, right...), node.Value)
}We recursively get values from the left child, then the right child, then add the current node's value.
Fill all three blanks to build a map of node values to their depths during postorder traversal.
func postorderDepthMap(node *Node, depth int, m map[int]int) {
if node == nil {
return
}
postorderDepthMap(node.[1], depth+1, m)
postorderDepthMap(node.[2], depth+1, m)
m[[3]] = depth
}We recurse on left and right children increasing depth, then store the node's value with its depth in the map.