C Postorder Traversal

Intermediate
6 min read
Updated: Nov 2026
1 example

Left, Right, Root

Postorder traversal visits the left subtree, the right subtree, then the root. Children are processed before their parent, which is exactly what you need to free a tree safely (a node must not be freed before its children) or to evaluate an expression tree.

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

Cheat Sheet

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

TopicDetail
OrderLeft, right, root
UseFree tree, evaluate expressions
TimeO(n)
Safe deleteChildren first

C Examples

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

Example 1 — Print and free in postorder

The same order is used for traversal and deletion.

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 postorder(const struct T *t) {
    if (!t) return;
    postorder(t->left);
    postorder(t->right);
    printf(" %d", t->key);
}

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

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

    printf("postorder:");
    postorder(root);
    printf("\nfreeing:");
    free_printing(root);
    printf("\n");
    return 0;
}

How It Works

  1. Leaves 4 and 5 come out before their parent 2.
  2. The root is last.
  3. The key is printed before free, so no freed memory is read.

Edge Cases & Pitfalls

Check these before relying on Postorder Traversal in real programs.

uaf

Freeing the parent first

Preorder deletion loses the child pointers.

read

Reading after free

Read t->key before free(t).

Key Takeaways

  • Children before parent.
  • Perfect for freeing.
  • Evaluates expression trees.

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

Frequently Asked Questions

A node can only be freed safely after its children are freed, which is the postorder order.
Visit the left subtree, the right subtree, then the node.
Continue with "C Level-order Traversal" next. Visit nodes level by level using a queue. The C Trees and Binary Search Trees hub lists every topic in order.

Next: Level-order Traversal

Visit nodes level by level using a queue.

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.

8 people found this page helpful