|
@@ -0,0 +1,53 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import "fmt"
|
|
|
+
|
|
|
+type node struct {
|
|
|
+ val int
|
|
|
+ left *node
|
|
|
+ right *node
|
|
|
+}
|
|
|
+
|
|
|
+func main() {
|
|
|
+ n8 := node{8, nil, nil}
|
|
|
+ n7 := node{7, nil, nil}
|
|
|
+ n6 := node{6, &n7, &n8}
|
|
|
+ n5 := node{5, nil, nil}
|
|
|
+ n4 := node{4, nil, &n6}
|
|
|
+ n3 := node{3, nil, &n5}
|
|
|
+ n2 := node{2, &n4, nil}
|
|
|
+ n1 := node{1, &n2, &n3}
|
|
|
+ fmt.Println("preorder: ")
|
|
|
+ preorder(&n1)
|
|
|
+ fmt.Println("\ninorder: ")
|
|
|
+ inorder(&n1)
|
|
|
+ fmt.Println("\npostorder: ")
|
|
|
+ postorder(&n1)
|
|
|
+}
|
|
|
+
|
|
|
+func preorder(root *node) {
|
|
|
+ if root == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Printf("%d ", root.val)
|
|
|
+ preorder(root.left)
|
|
|
+ preorder(root.right)
|
|
|
+}
|
|
|
+
|
|
|
+func inorder(root *node) {
|
|
|
+ if root == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ inorder(root.left)
|
|
|
+ fmt.Printf("%d ", root.val)
|
|
|
+ inorder(root.right)
|
|
|
+}
|
|
|
+
|
|
|
+func postorder(root *node) {
|
|
|
+ if root == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ postorder(root.left)
|
|
|
+ postorder(root.right)
|
|
|
+ fmt.Printf("%d ", root.val)
|
|
|
+}
|