Problem
Section titled “Problem”Given the root of a binary tree, return the preorder traversal of its nodes’ values.
Example 1:

Input: root = [1,null,2,3]Output: [1,2,3]Example 2:

Input: root = [1,2,3,4,5,null,8,null,null,6,7,9]Output: [1,2,4,5,6,7,3,8,9]Example 3:
Input: root = []Output: []Example 4:
Input: root = [1]Output: [1]Constraints:
- The number of nodes in the tree is in the range
[0, 100]. -
Node.val
Follow-up: Recursive solution is trivial, could you do it iteratively?
Solution approach
Section titled “Solution approach”Preorder means: the node, then its whole left subtree, then its whole right subtree.
Approach 1: Explicit stack
Section titled “Approach 1: Explicit stack”Intuition: A node is output as soon as it is reached, and afterwards we owe two pieces of work: the left subtree, then the right subtree. Push both onto a stack — right first, then left — so that the left subtree, being on top, is fully processed before the right child surfaces. The stack therefore always holds the roots of the subtrees still to be visited, in the correct order.
Algorithm:
- If
rootis null, return[]. Otherwise pushroot. - While
stis not empty: popnode, appendnode->val, then pushnode->rightandnode->leftif they exist.
On Example 2: pop 1 (push 3, 2), pop 2 (push 5, 4), pop 4, pop 5 (push 7, 6), pop 6, pop 7, pop 3 (push 8), pop 8 (push 9), pop 9 — giving [1,2,4,5,6,7,3,8,9].
Complexity: time — each node is pushed and popped once. space — the stack holds at most one pending right child per level plus the current node.
Approach 2: Morris traversal
Section titled “Approach 2: Morris traversal”Intuition: This is the Morris inorder traversal with the output moved. To return to cur after its left subtree without a stack, temporarily set the right pointer of its inorder predecessor prev (the rightmost node of cur->left, whose right is always null) to cur. Every node with a left child is therefore reached twice: once going down (when we create the thread) and once coming back (when we find the thread). Preorder outputs a node the first time it is reached, so we output cur when we create the thread, and only remove the thread on the second visit.
Algorithm:
- Set
cur = root. Whilecuris not null:- If
cur->leftis null: appendcur->valand movecur = cur->right. - Otherwise find
prev: start atcur->leftand go right whileprev->rightis neither null norcur.- If
prev->rightis null (first visit): appendcur->val, setprev->right = cur, and movecur = cur->left. - If
prev->right == cur(back through the thread): setprev->right = nullptrand movecur = cur->right.
- If
- If
Complexity: time — each edge is walked a constant number of times while creating and removing threads. extra space; the tree is restored when the loop ends.
Edge cases
Section titled “Edge cases”- Empty tree (Example 3): Approach 1 returns early; in Approach 2 the loop never runs.
- Node with no left child (the root of Example 1): it is output immediately and the walk moves right; no thread is created.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<int> order; if (!root) return order; stack<TreeNode*> st; st.push(root); while (!st.empty()) { TreeNode* node = st.top(); st.pop(); order.push_back(node->val); if (node->right) st.push(node->right); if (node->left) st.push(node->left); } return order; }};class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> order = new ArrayList<>(); if (root == null) return order; ArrayDeque<TreeNode> st = new ArrayDeque<>(); st.push(root); while (!st.isEmpty()) { TreeNode node = st.pop(); order.add(node.val); if (node.right != null) st.push(node.right); if (node.left != null) st.push(node.left); } return order; }}class Solution: def preorderTraversal(self, root: TreeNode | None) -> list[int]: order = [] if not root: return order st = [root] while st: node = st.pop() order.append(node.val) if node.right: st.append(node.right) if node.left: st.append(node.left) return orderclass Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<int> order; TreeNode* cur = root; while (cur) { if (!cur->left) { order.push_back(cur->val); cur = cur->right; continue; } TreeNode* prev = cur->left; while (prev->right && prev->right != cur) prev = prev->right; if (!prev->right) { order.push_back(cur->val); // first visit prev->right = cur; cur = cur->left; } else { prev->right = nullptr; // second visit: remove the thread cur = cur->right; } } return order; }};class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> order = new ArrayList<>(); TreeNode cur = root; while (cur != null) { if (cur.left == null) { order.add(cur.val); cur = cur.right; continue; } TreeNode prev = cur.left; while (prev.right != null && prev.right != cur) prev = prev.right; if (prev.right == null) { order.add(cur.val); // first visit prev.right = cur; cur = cur.left; } else { prev.right = null; // second visit: remove the thread cur = cur.right; } } return order; }}class Solution: def preorderTraversal(self, root: TreeNode | None) -> list[int]: order = [] cur = root while cur: if not cur.left: order.append(cur.val) cur = cur.right continue prev = cur.left while prev.right and prev.right is not cur: prev = prev.right if not prev.right: order.append(cur.val) # first visit prev.right = cur cur = cur.left else: prev.right = None # second visit: remove the thread cur = cur.right return orderGive it a try first
Attempt the problem on LeetCode — the code will still be here.