Complete the code to print the root node's value first in preorder traversal.
func preorder(node *Node) {
if node == nil {
return
}
fmt.Println(node.[1])
}In preorder traversal, we visit the root node first, so we print node.Value.
Complete the code to recursively traverse the left subtree in preorder.
func preorder(node *Node) {
if node == nil {
return
}
fmt.Println(node.Value)
preorder(node.[1])
}After visiting the root, preorder traversal visits the left subtree by calling preorder(node.Left).
Fix the error in the code to correctly traverse the right subtree in preorder.
func preorder(node *Node) {
if node == nil {
return
}
fmt.Println(node.Value)
preorder(node.Left)
preorder(node.[1])
}After visiting the left subtree, preorder traversal visits the right subtree by calling preorder(node.Right).
Fill both blanks to print the root node's value first and then recursively traverse the left subtree in preorder.
func preorder(node *Node) {
if node == nil {
return
}
fmt.Println(node.[1])
preorder(node.[2])
}First print the root's value with node.Value, then recursively traverse the left subtree with preorder(node.Left).
Fill all three blanks to complete the preorder traversal visiting root, left, then right.
func preorder(node *Node) {
if node == nil {
return
}
fmt.Println(node.[1])
preorder(node.[2])
preorder(node.[3])
}Preorder traversal visits the root node's value, then recursively visits the left subtree, then the right subtree.