LeetCode #144 Easy

Binary Tree Preorder Traversal

Given the root of a binary tree, return the preorder traversal of its node values: the node first, then its left subtree, then its right subtree.

binary treedfsrecursion
Open on LeetCode ↗
02

Intuition

💡

Preorder visits a node the moment it's discovered — record first, explore later. It's the order you'd photocopy a tree: root, then everything under the left child, then everything under the right.

03

Approach

1

Visit before you descend

The only difference from inorder is where the append sits: record node.val before the two recursive calls. That single line move changes the output order completely — a good reminder that all three DFS traversals are the same walk, differing only in when they look at the node.

2

Why preorder matters

Because the root comes first, preorder is the natural order for copying or serializing a tree — you can rebuild the tree by reading the list left to right. It's also plain DFS order, the same sequence a stack-based explorer would discover nodes in.

3

Iterative version is the easiest of the three

Push root; loop: pop, visit, push right then left (right first so left pops first). No revisiting logic needed since the node is handled the moment it's popped.

04

Solution & live demo

python
1class Solution:
2 def preorderTraversal(self, root):
3 res = []
4 def dfs(node):
5 if not node:
6 return
7 res.append(node.val)
8 dfs(node.left)
9 dfs(node.right)
10 dfs(root)
11 return res
05

Edge cases

Empty tree

Base case returns immediately — empty list.

Only right children

Each node is visited then recursion slides right; output equals the top-to-bottom chain.

Single node

Visited immediately; both child calls hit the base case.

06

Complexity

Time
O(n)
Space
O(h)
One visit per node; stack depth bounded by tree height.