Bird
Raised Fist0
CHow-ToBeginner · 4 min read

How to Traverse Binary Tree in C: Syntax and Examples

To traverse a binary tree in C, use recursive functions for preorder, inorder, or postorder traversal. Each method visits nodes in a specific order by recursively visiting left and right children and processing the current node.
📐

Syntax

Binary tree traversal in C typically uses recursive functions with this pattern:

  • Preorder: Visit current node, then left subtree, then right subtree.
  • Inorder: Visit left subtree, then current node, then right subtree.
  • Postorder: Visit left subtree, then right subtree, then current node.

The function takes a pointer to the current node and processes nodes accordingly.

c
void preorder(struct Node* root) {
    if (root == NULL) return;
    printf("%d ", root->data);
    preorder(root->left);
    preorder(root->right);
}

void inorder(struct Node* root) {
    if (root == NULL) return;
    inorder(root->left);
    printf("%d ", root->data);
    inorder(root->right);
}

void postorder(struct Node* root) {
    if (root == NULL) return;
    postorder(root->left);
    postorder(root->right);
    printf("%d ", root->data);
}
💻

Example

This example creates a simple binary tree and shows all three traversal methods printing node values.

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

struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

struct Node* newNode(int data) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return node;
}

void preorder(struct Node* root) {
    if (root == NULL) return;
    printf("%d ", root->data);
    preorder(root->left);
    preorder(root->right);
}

void inorder(struct Node* root) {
    if (root == NULL) return;
    inorder(root->left);
    printf("%d ", root->data);
    inorder(root->right);
}

void postorder(struct Node* root) {
    if (root == NULL) return;
    postorder(root->left);
    postorder(root->right);
    printf("%d ", root->data);
}

int main() {
    struct Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);

    printf("Preorder traversal: ");
    preorder(root);
    printf("\n");

    printf("Inorder traversal: ");
    inorder(root);
    printf("\n");

    printf("Postorder traversal: ");
    postorder(root);
    printf("\n");

    return 0;
}
Output
Preorder traversal: 1 2 4 5 3 Inorder traversal: 4 2 5 1 3 Postorder traversal: 4 5 2 3 1
⚠️

Common Pitfalls

Common mistakes when traversing binary trees in C include:

  • Not checking for NULL before accessing node pointers, causing crashes.
  • Mixing traversal orders and printing nodes in the wrong sequence.
  • Forgetting to free allocated memory (not shown here but important in real programs).

Always ensure base case if (root == NULL) return; is present to stop recursion.

c
/* Wrong: Missing NULL check causes crash */
void wrongPreorder(struct Node* root) {
    printf("%d ", root->data); // crash if root is NULL
    wrongPreorder(root->left);
    wrongPreorder(root->right);
}

/* Correct: Check for NULL first */
void correctPreorder(struct Node* root) {
    if (root == NULL) return;
    printf("%d ", root->data);
    correctPreorder(root->left);
    correctPreorder(root->right);
}
📊

Quick Reference

Traversal order summary:

TraversalOrder of Visiting Nodes
PreorderCurrent, Left, Right
InorderLeft, Current, Right
PostorderLeft, Right, Current
TraversalOrder of Visiting Nodes
PreorderCurrent, Left, Right
InorderLeft, Current, Right
PostorderLeft, Right, Current
✅

Key Takeaways

Always check for NULL before accessing node pointers to avoid crashes.
Use recursion to visit nodes in preorder, inorder, or postorder sequence.
Preorder visits current node before children; inorder visits current node between children; postorder visits current node after children.
Free allocated memory in real applications to avoid memory leaks.
Practice with simple trees to understand traversal order clearly.