Algo-Sim › Binary Trees & BST
Preorder Traversal (Recursive)
Easy · Binary Trees & BST · step through it in Java, Python and C++ on your own input
Recursive depth-first traversal that outputs each node before either subtree.
Problem
Given the root of a binary tree, return its values in preorder: the node itself, then its whole left subtree, then its whole right subtree. This version recurses.
Constraints
- 0 ≤ number of nodes ≤ 100
- -100 ≤ Node.val ≤ 100
- Recursion depth equals the tree height
Edge cases to test
- Empty tree:
[] - A left-only chain (the recursion is as deep as the tree)
- One node
Follow-up questions
- Can you do it without recursion?
- Use an explicit stack: pop, output, push right then left. Same O(n) time and O(h) space, with no risk of stack overflow.
- What uses preorder?
- Copying or serialising a tree: the root comes first, so reading the values back rebuilds it top-down (with null markers).
Solution
Java
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> out = new ArrayList<>();
visit(root, out);
return out;
}
private void visit(TreeNode node, List<Integer> out) {
if (node == null) return;
out.add(node.val);
visit(node.left, out);
visit(node.right, out);
}Python
def preorder_traversal(root):
out = []
def visit(node):
if not node:
return
out.append(node.val)
visit(node.left)
visit(node.right)
visit(root)
return outC++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> out;
visit(root, out);
return out;
}
void visit(TreeNode *node, vector<int> &out) {
if (!node)
return;
out.push_back(node->val);
visit(node->left, out);
visit(node->right, out);
}
};
More Binary Trees & BST problems
- Inorder Traversal (Recursive) (Easy)
- Postorder Traversal (Recursive) (Easy)
- Preorder Traversal (Iterative) (Easy)
- Inorder Traversal (Iterative) (Easy)
- Postorder Traversal (Iterative) (Easy)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Binary Tree Right Side View (Medium)
- Vertical Order Traversal of a Binary Tree (Hard)
- Maximum Depth of Binary Tree (Easy)
- Diameter of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Invert Binary Tree (Easy)
- Flatten Binary Tree to Linked List (Medium)
- Same Tree (Easy)
- Symmetric Tree (Easy)
- Subtree of Another Tree (Easy)
- Path Sum (Easy)
- Path Sum II (Medium)
- Path Sum III (Medium)
- Count Good Nodes in Binary Tree (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Kth Ancestor of a Tree Node (Hard)
- All Nodes Distance K in Binary Tree (Medium)
- Validate Binary Search Tree (Medium)
- Search in a Binary Search Tree (Easy)
- Insert into a Binary Search Tree (Medium)
- Delete Node in a BST (Medium)
- Kth Smallest Element in a BST (Medium)
- Binary Search Tree Iterator (Medium)
- Convert Sorted Array to Binary Search Tree (Easy)
- AVL Tree Insert (Hard)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Binary Tree Maximum Path Sum (Hard)
- Serialize and Deserialize Binary Tree (Hard)