Last active
August 16, 2016 06:53
-
-
Save xy7313/d98cf6bcee4e15058201c6b5fcff9f48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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