Problem List

Binary Tree Preorder Traversal

May 30, 2025Go stack, tree, depth first search, binary treeeasy

Problem

Reflections

Memorized at this point.

Performance

Complexity

Go Solution
func preorderTraversal(root *TreeNode) []int {
    var res []int
    var dfs func(root *TreeNode) 
    dfs = func(root *TreeNode) {
        if root == nil {
            return 
        }

        res = append(res, root.Val)
        dfs(root.Left)
        dfs(root.Right)
    }

    dfs(root)

    return res
}
LeetCode Problem Link