From b0f60073c8b1c64612fe6445358b4eeaf112394d Mon Sep 17 00:00:00 2001 From: gbabyX Date: Thu, 12 Nov 2020 10:26:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E5=89=8D?= =?UTF-8?q?=E5=BA=8F=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 202011/btree/pre.go | 21 +++++++++++++++++++++ 202011/btree/tree.go | 7 +++++++ 2 files changed, 28 insertions(+) create mode 100644 202011/btree/pre.go create mode 100644 202011/btree/tree.go 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 +}