leet-code/binary-tree-preorder-traversal/sol.go

31 lines
555 B
Go
Raw Permalink Normal View History

2022-06-09 14:45:14 +00:00
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func preorderTraversalInPlace(root *TreeNode, list *[]int) {
if root == nil {
return
}
*list = append(*list, root.Val)
preorderTraversalInPlace(root.Left, list)
preorderTraversalInPlace(root.Right, list)
}
func preorderTraversal(root *TreeNode) []int {
list := &[]int{}
preorderTraversalInPlace(root, list)
return *list
}