Skip to content

Instantly share code, notes, and snippets.

@xy7313
Last active August 16, 2016 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xy7313/d98cf6bcee4e15058201c6b5fcff9f48 to your computer and use it in GitHub Desktop.
Save xy7313/d98cf6bcee4e15058201c6b5fcff9f48 to your computer and use it in GitHub Desktop.
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
//一开始自己写的有个问题是result := append([]int{}, root.Val)这里,居然没理解root.Val是什么。。。醉了
//查到下面代码才改对的
func preorderTraversal(root *TreeNode) []int {
if root==nil{
return nil
}
result := append([]int{}, root.Val)
if left:=root.Left;left!=nil{
result=append(result,preorderTraversal(left)...)
}
if right:=root.Right;right!=nil{
result=append(result,preorderTraversal(right)...)
}
return result
}
//git 4396
func preorderTraversal(root *TreeNode) []int {
if root == nil {
return nil
}
result := append([]int{}, root.Val)
if left := preorderTraversal(root.Left); left != nil {
result = append(result, left...)
}
if right := preorderTraversal(root.Right); right != nil {
result = append(result,right...)
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment