Open In App

Postorder Traversal of Binary Tree

Last Updated : 07 Oct, 2025
Comments
Improve
Suggest changes
6 Likes
Like
Report

Given a root of the binary tree, return the postorder traversal of the binary tree.

Postorder Traversal is a method to traverse a tree such that for each node, you first traverse its left subtree, then its right subtree, and finally visit the node itself.

Examples:

Input:

20

Output: [2, 3, 1]
Explanation: Postorder Traversal visits the nodes in the following order: Left, Right, Root. Therefore, we visit the left node 2, then the right node 3 and lastly the root node 1.

Input:

24

Output: [4, 5, 2, 6, 3, 1]
Explanation: Postorder Traversal visits the nodes in the following order: Left, Right, Root. Therefore resulting is 4 , 5, 2, 6, 3, 1.

[Approach] Using Recursion

The main idea is to traverse the tree recursively, starting from the root node, and first completely traverse the left subtree, then completely traverse the right subtree, and finally visit the root node.

How does Postorder Traversal work?

C++
#include <iostream>
#include <vector>
using namespace std;

// Node Structure
class Node {
public:
    int data;
    Node *left;
    Node *right;

    Node(int v)
    {
        data = v;
        left = right = nullptr;
    }
};

void postOrder(Node *node, vector<int> &res)
{
    if (node == nullptr)
        return;

    // First we traverse left subtree
    postOrder(node->left, res);

    // After visiting left, traverse right subtree
    postOrder(node->right, res);

    // now we visit node
    res.push_back(node->data);
}

int main()
{
    //Represent Tree
    //       1
    //      / \
    //     2   3
    //    / \   \
    //   4   5   6
    Node *root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->left = new Node(4);
    root->left->right = new Node(5);
    root->right->right = new Node(6);

    vector<int> result;
    postOrder(root, result); 

    // Print the postorder
    for (int val : result)
        cout << val << " ";

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

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

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

void postOrder(struct Node *node)
{
    if (node == NULL)
        return;

    // First we traverse left subtree
    postOrder(node->left);

    // After visiting left, traverse right subtree
    postOrder(node->right);

    // now we visit node
    printf("%d ", node->data);
}

int main()
{
    //Represent Tree
    //       1
    //      / \
    //     2   3
    //    / \   \
    //   4   5   6

    struct Node *root = createNode(1);
    root->left = createNode(2);
    root->right = createNode(3);
    root->left->left = createNode(4);
    root->left->right = createNode(5);
    root->right->right = createNode(6);
    postOrder(root);

    return 0;
}
Java
import java.util.ArrayList;

// Node Structure
class Node {
    int data;
    Node left;
    Node right;

    Node(int v) {
        data = v;
        left = null;
        right = null;
    }
}

public class GFG {
    static void postOrder(Node node, ArrayList<Integer> res) {
        if (node == null)
            return;

        // First we traverse left subtree
        postOrder(node.left, res);

        // After visiting left, traverse right subtree
        postOrder(node.right, res);

        // now we visit node
        res.add(node.data);
    }

    public static void main(String[] args) {

        //Represent Tree
        //       1
        //      / \
        //     2   3
        //    / \   \
        //   4   5   6
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        root.right.right = new Node(6);

        ArrayList<Integer> result = new ArrayList<>();
        postOrder(root, result);

        // Print the postorder
        for (int val : result)
            System.out.print(val + " ");
    }
}
Python
# Node Structure
class Node:
    def __init__(self, v):
        self.data = v
        self.left = None
        self.right = None

def postOrder(node, res):
    if node is None:
        return

    # First we traverse left subtree
    postOrder(node.left, res)

    # After visiting left, traverse right subtree
    postOrder(node.right, res)

    # now we visit node
    res.append(node.data)

if __name__ == "__main__":
    #Represent Tree
    #       1
    #      / \
    #     2   3
    #    / \   \
    #   4   5   6
    root = Node(1)
    root.left = Node(2)
    root.right = Node(3)
    root.left.left = Node(4)
    root.left.right = Node(5)
    root.right.right = Node(6)

    result = []
    postOrder(root, result)

    # Print the postorder
    for val in result:
        print(val, end=" ")
C#
using System;
using System.Collections.Generic;

// Node Structure
class Node
{
    public int data;
    public Node left;
    public Node right;

    public Node(int v)
    {
        data = v;
        left = null;
        right = null;
    }
}

class GFG
{
    static void postOrder(Node node, List<int> res)
    {
        if (node == null)
            return;

        // First we traverse left subtree
        postOrder(node.left, res);

        // After visiting left, traverse right subtree
        postOrder(node.right, res);

        // now we visit node
        res.Add(node.data);
    }

    static void Main()
    {
        //Represent Tree
        //       1
        //      / \
        //     2   3
        //    / \   \
        //   4   5   6
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        root.right.right = new Node(6);

        List<int> result = new List<int>();
        postOrder(root, result);

        // Print the postorder
        foreach (int val in result)
            Console.Write(val + " ");
    }
}
JavaScript
// Node Structure
class Node {
    constructor(v) {
        this.data = v;
        this.left = null;
        this.right = null;
    }
}

function postOrder(node, res) {
    if (node === null)
        return;

    // First we traverse left subtree
    postOrder(node.left, res);

    // After visiting left, traverse right subtree
    postOrder(node.right, res);

    // now we visit node
    res.push(node.data);
}

// Driver code
 //Represent Tree
        //       1
        //      / \
        //     2   3
        //    / \   \
        //   4   5   6
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);

let result = [];
postOrder(root, result);

// Print the postorder
for (let val of result)
    process.stdout.write(val + " ");

Output
4 5 2 6 3 1 

Time Complexity: O(n)
Auxiliary Space: O(h), h is the height of the tree

  • In the worst case, h can be the same as n (when the tree is a skewed tree)
  • In the best case, h can be the same as log n (when the tree is a complete tree)

Key Properties:

Related articles:


Postorder Traversal in Python
Visit Course explore course icon
Article Tags :

Explore