Postorder Recursive Traversal of a Given Binary Tree in C++



A binary tree is a tree data structure where each node has zero, one, or two children. Even an empty tree is called a valid binary tree. Our goal is to write a C++ program to perform postorder recursive traversal on a given binary tree.

Traversal means visiting all the nodes in a tree exactly once. In postorder traversal, we visit the left child (left subtree) first, then the right child (right subtree), and finally the root node.

Let's take a small binary tree and look at an example.

Here, we first go deep into the leftmost part(5), come back and visit 2, then 6. After the left subtree is done, we go to the right subtree (9 -> 8 -> 4), and finally visit the root(3).

Postorder Traversal with Recursion

We perform postorder traversal using recursion. Recursion is when a function calls itself with a smaller value until it reaches the base case. To do a postorder traversal, we first call the function on the left child, then on the right child, and finally visit the root.

Here's how we do it:

  • First, we define a function called PostOrder() that takes the root node as input.
  • Then, we check if the node is NULL. If it is, we return. This is our base case.
  • Next, if the node is not NULL, we call PostOrder() on the left child.
  • After that, we call it on the right child.
  • Finally, we print the current node's value.

This way, we visit the nodes in the proper order: left subtree -> right subtree -> root.

Example

Here's a complete C++ program to perform postorder traversal using recursion.

#include <iostream>
using namespace std;

// Structure for binary tree node
struct BinaryTreeNode {
    int data;  // Node value
    BinaryTreeNode* left;  // Pointer to left child
    BinaryTreeNode* right; // Pointer to right child

    // Constructor to initialize node
    BinaryTreeNode(int value) {
        data = value;         // Set data
        left = right = nullptr; // Initialize children to nullptr
    }
};

// Recursive function for postorder traversal
void PostOrder(BinaryTreeNode* root) {
    if (root != nullptr) {            
        PostOrder(root->left);        // Recursively visit left subtree
        PostOrder(root->right);       // Recursively visit right subtree
        cout << root->data << " ";    // Visit the current node (print value)
    }
}

int main() {
    // Constructing the binary tree
    BinaryTreeNode* root = new BinaryTreeNode(3);  // Root node with value 3
    root->left = new BinaryTreeNode(6);            // Left child of root
    root->right = new BinaryTreeNode(5);           // Right child of root
    root->left->left = new BinaryTreeNode(2);      // Left child of node 6
    root->left->right = new BinaryTreeNode(4);     // Right child of node 6
    root->right->right = new BinaryTreeNode(9);    // Right child of node 5
    root->right->right->left = new BinaryTreeNode(8); // Left child of node 9

    // Perform postorder traversal
    cout << "Postorder Traversal: ";  // Output heading
    PostOrder(root);                  // Call postorder traversal
    cout << endl;                     // Newline after output

    return 0;  // End of program
}

Once you run the above program, you will see the following output ,which shows the tree is traversed using postOrder traversal.

Postorder Traversal: 2 4 6 8 9 5 3 

Time Complexity: O(n), where n is the number of nodes in the tree. Each node is visited only once.

Space Complexity: O(h), where h is the height of the tree, because of the recursive call stack.

Updated on: 2025-05-16T18:37:48+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements