diff --git a/202011/btree/pre.go b/202011/btree/pre.go new file mode 100644 index 0000000..fbc66a8 --- /dev/null +++ b/202011/btree/pre.go @@ -0,0 +1,21 @@ +package btree + +func preorderTraversal(root *TreeNode) []int { + if root == nil { + return nil + } + var l []int + preoder(root, &l) + return l +} + +func preoder(node *TreeNode, l *[]int) { + v := node.Val + *l = append(*l, v) + if node.Left != nil { + preoder(node.Left, l) + } + if node.Right != nil { + preoder(node.Right, l) + } +} diff --git a/202011/btree/tree.go b/202011/btree/tree.go new file mode 100644 index 0000000..5c43721 --- /dev/null +++ b/202011/btree/tree.go @@ -0,0 +1,7 @@ +package btree + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +}