Skip to content

Commit

Permalink
二叉树的前序遍历
Browse files Browse the repository at this point in the history
  • Loading branch information
gbabyX committed Nov 12, 2020
1 parent 23c4584 commit b0f6007
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 202011/btree/pre.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
7 changes: 7 additions & 0 deletions 202011/btree/tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package btree

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

0 comments on commit b0f6007

Please sign in to comment.