package main
import "fmt"
type Node struct {
Val int
Left *Node
Right *Node
}
func main() {
// Step 1: Create root node
root := &Node{Val: 1}
// Step 2: Create left child of root
root.Left = &Node{Val: 2}
// Step 3: Create right child of root
root.Right = &Node{Val: 3}
// Step 4: Create left child of node 2
root.Left.Left = &Node{Val: 4}
// Step 5: Create right child of node 2
root.Left.Right = &Node{Val: 5}
printTree(root, "")
}
func printTree(node *Node, prefix string) {
if node == nil {
return
}
fmt.Println(prefix + fmt.Sprint(node.Val))
printTree(node.Left, prefix+" ")
printTree(node.Right, prefix+" ")
}
Create root node with value 1
root.Left = &Node{Val: 2}
Link left child node with value 2 to root
root.Right = &Node{Val: 3}
Link right child node with value 3 to root
root.Left.Left = &Node{Val: 4}
Link left child node with value 4 to node 2
root.Left.Right = &Node{Val: 5}
Link right child node with value 5 to node 2