Skip to content

Latest commit

 

History

History
30 lines (25 loc) · 512 Bytes

binary-tree-preorder-traversal.md

File metadata and controls

30 lines (25 loc) · 512 Bytes

Code

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */

func preorder(root *TreeNode, traversal []int) []int {
	if root == nil {
		return traversal
	}

	traversal = append(traversal, root.Val)
	traversal = preorder(root.Left, traversal)
	traversal = preorder(root.Right, traversal)

	return traversal
}

func preorderTraversal(root *TreeNode) []int {
	traversal := preorder(root, []int{})
	return traversal
}