C Preorder Traversal

Intermediate
6 min read
Updated: Nov 2026
1 example

Root, Left, Right

Preorder traversal visits the root, then the left subtree, then the right subtree. It is useful for copying a tree and for writing a tree to a file because the parent appears before its children. An iterative version uses an explicit stack: push the right child first so the left is processed first.

Remember
void preorder(const struct T *t) {
    if (!t) return;
    visit(t);
    preorder(t->left);
    preorder(t->right);
}

Cheat Sheet

Keep these facts handy while you work with Preorder Traversal in C.

TopicDetail
OrderRoot, left, right
UseCopy or serialize a tree
TimeO(n)
IterativeStack, push right then left

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Inorder Traversal.

Example 1 — Recursive and iterative preorder

Both versions give the same order.

C
#include <stdio.h>
#include <stdlib.h>

struct T {
    int key;
    struct T *left, *right;
};

static struct T *mk(int k, struct T *l, struct T *r) {
    struct T *t = malloc(sizeof *t);
    if (!t) exit(1);
    t->key = k;
    t->left = l;
    t->right = r;
    return t;
}

static void free_tree(struct T *t) {
    if (!t) return;
    free_tree(t->left);
    free_tree(t->right);
    free(t);
}

static void preorder(const struct T *t) {
    if (!t) return;
    printf(" %d", t->key);
    preorder(t->left);
    preorder(t->right);
}

static void preorder_iter(const struct T *root) {
    const struct T *st[32];
    int top = -1;
    if (root) st[++top] = root;
    while (top >= 0) {
        const struct T *n = st[top--];
        printf(" %d", n->key);
        if (n->right) st[++top] = n->right;
        if (n->left)  st[++top] = n->left;
    }
}

int main(void) {
    struct T *root = mk(1, mk(2, mk(4, NULL, NULL), mk(5, NULL, NULL)), mk(3, NULL, NULL));

    printf("recursive:");
    preorder(root);
    printf("\niterative:");
    preorder_iter(root);
    printf("\n");

    free_tree(root);
    return 0;
}

How It Works

  1. The root 1 is printed first, then the whole left subtree 2, 4, 5.
  2. The right subtree 3 comes last.
  3. The iterative version pushes right before left so left pops first.

Edge Cases & Pitfalls

Check these before relying on Preorder Traversal in real programs.

order

Pushing left before right

In the iterative version this visits the right subtree first.

depth

Deep recursion

A very skewed tree can overflow the call stack.

Key Takeaways

  • Root first.
  • Parents before children.
  • Iterative needs a stack.

One line: Preorder traversal visits the root, then the left subtree, then the right subtree.

Frequently Asked Questions

A depth-first traversal that visits the root, then the left subtree, then the right subtree.
To copy a tree, serialize it, or produce a prefix expression from an expression tree.
Continue with "C Inorder Traversal" next. Visit left, root, then right; on a BST it yields sorted keys. The C Trees and Binary Search Trees hub lists every topic in order.

Next: Inorder Traversal

Visit left, root, then right; on a BST it yields sorted keys.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful