需要传地址,例如:
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
import "fmt"
func inorder(root *TreeNode, list *[] int) {
if root == nil {
return
}
inorder(root.Left, list)
*list = append(*list, root.Val)
inorder(root.Right, list)
}
func inorderTraversal(root *TreeNode) []int {
var list[] int
inorder(root, &list)
return list
}
参考资料: