Postorder Traversal of Binary Tree Last Updated : 28 Mar, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Postorder traversal is a tree traversal method that follows the Left-Right-Root order:The left subtree is visited first.The right subtree is visited next.The root node is processed last.How does Postorder Traversal work?Key Properties:It is used for tree deletion because subtrees are deleted before the current node.It is also useful for generating the postfix expression from an expression tree.Examples:Input:Output: 2 3 1Explanation: 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:Output: 4 5 2 6 3 1Explanation: Postorder Traversal (Left → Right → Root). Visit 4 → 5 → 2 → 6 → 3 → 1 , resulting in 4 5 2 6 3 1.Algorithm:If root is NULL then returnRecursively traverse the left subtree.Recursively traverse the right subtree.Process the root node (e.g., print its value). C++ #include <bits/stdc++.h> using namespace std; // Structure of a Binary Tree Node struct Node { int data; struct Node *left, *right; Node(int v) { data = v; left = right = nullptr; } }; // Function to print postorder traversal void printPostorder(struct Node* node) { if (node == nullptr) return; // First recur on left subtree printPostorder(node->left); // Then recur on right subtree printPostorder(node->right); // Now deal with the node cout << node->data << " "; } int main() { struct 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); printPostorder(root); return 0; } C #include <stdio.h> #include <stdlib.h> // Structure of a Binary Tree Node struct Node { int data; struct Node *left, *right; }; // Function to print postorder traversal void printPostorder(struct Node* node) { if (node == NULL) return; // First recur on left subtree printPostorder(node->left); // Then recur on right subtree printPostorder(node->right); // Now deal with the node printf("%d ", node->data); } int main() { struct Node* root = (struct Node*)malloc(sizeof(struct Node)); root->data = 1; root->left = (struct Node*)malloc(sizeof(struct Node)); root->left->data = 2; root->right = (struct Node*)malloc(sizeof(struct Node)); root->right->data = 3; root->left->left = (struct Node*)malloc(sizeof(struct Node)); root->left->left->data = 4; root->left->right = (struct Node*)malloc(sizeof(struct Node)); root->left->right->data = 5; root->right->right = (struct Node*)malloc(sizeof(struct Node)); root->right->right->data = 6; printPostorder(root); return 0; } Java class Node { int data; Node left, right; Node(int v) { data = v; left = right = null; } } public class GfG{ // Function to print postorder traversal void printPostorder(Node node) { if (node == null) return; // First recur on left subtree printPostorder(node.left); // Then recur on right subtree printPostorder(node.right); // Now deal with the node System.out.print(node.data + " "); } public static void main(String[] args) { 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); GfG tree = new GfG(); tree.printPostorder(root); } } Python class Node: def __init__(self, v): self.data = v self.left = None self.right = None # Function to print postorder traversal def print_postorder(node): if node is None: return # First recur on left subtree print_postorder(node.left) # Then recur on right subtree print_postorder(node.right) # Now deal with the node print(node.data, end=' ') if __name__ == '__main__': 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) print_postorder(root) C# using System; // Structure of a Binary Tree Node public class Node { public int data; public Node left, right; public Node(int v) { data = v; left = right = null; } } public class GfG { // Function to print postorder traversal static void printPostorder(Node node) { if (node == null) return; // First recur on left subtree printPostorder(node.left); // Then recur on right subtree printPostorder(node.right); // Now deal with the node Console.Write(node.data + " "); } static public void Main() { 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); printPostorder(root); } } JavaScript // Structure of a Binary Tree Node class Node { constructor(v) { this.data = v; this.left = null; this.right = null; } } // Function to print postorder traversal function printPostorder(node) { if (node == null) { return; } // First recur on left subtree printPostorder(node.left); // Then recur on right subtree printPostorder(node.right); // Now deal with the node console.log(node.data + " "); } // Driver code function main() { 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); printPostorder(root); } main(); Output4 5 2 6 3 1 Time Complexity: O(n)Auxiliary Space: O(h), h is the height of the treeIn 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)Related articles:Types of Tree traversalsIterative Postorder traversal (using two stacks)Iterative Postorder traversal (using one stack)Postorder of Binary Tree without recursion and without stackFind Postorder traversal of BST from preorder traversalMorris traversal for PostorderPrint postorder traversal from preoreder and inorder traversal Comment More infoAdvertise with us Next Article Postorder Traversal of Binary Tree A animeshdey Follow Improve Article Tags : Tree DSA Practice Tags : Tree Similar Reads Preorder Traversal of Binary Tree Preorder traversal is a tree traversal method that follows the Root-Left-Right order:The root node of the subtree is visited first.Next, the left subtree is recursively traversed.Finally, the right subtree is recursively traversed.How does Preorder Traversal work?Key Properties: Used in expression t 5 min read Mix Order Traversal of a Binary Tree Given a Binary Tree consisting of N nodes, the task is to print its Mix Order Traversal. Mix Order Traversal is a tree traversal technique, which involves any two of the existing traversal techniques like Inorder, Preorder and Postorder Traversal. Any two of them can be performed or alternate levels 13 min read Double Order Traversal of a Binary Tree Given a Binary Tree, the task is to find its Double Order Traversal. Double Order Traversal is a tree traversal technique in which every node is traversed twice in the following order: Visit the Node.Traverse the Left Subtree.Visit the Node.Traverse the Right Subtree.Examples:Input: Output: 1 7 4 4 6 min read Inorder Traversal of Binary Tree Inorder traversal is a depth-first traversal method that follows this sequence:Left subtree is visited first.Root node is processed next.Right subtree is visited last.How does Inorder Traversal work?Key Properties:If applied to a Binary Search Tree (BST), it returns elements in sorted order.Ensures 5 min read Triple Order Traversal of a Binary Tree Given a Binary Tree, the task is to find its Triple Order Traversal. Triple Order Traversal is a tree traversal technique in which every node is traversed thrice in the following order: Visit the root nodeTraverse the left subtreeVisit the root nodeTraverse the right subtreeVisit the root node.Examp 7 min read Binary Tree Traversal Binary trees are fundamental data structures in computer science and understanding their traversal is crucial for various applications. Traversing a binary tree means visiting all the nodes in a specific order. There are several traversal methods, each with its unique applications and benefits. This 15+ min read Binary Tree Iterator for Inorder Traversal Given a Binary Tree and an input array. The task is to create an Iterator that utilizes next() and hasNext() functions to perform Inorder traversal on the binary tree. Examples: Input: 8 Input Array = [next(), hasNext(), next(), next(), next(), hasNext(), next(), next(), hasNext()] / \ 3 9 / \ 2 4 \ 13 min read Inorder traversal of an N-ary Tree Given an N-ary tree containing, the task is to print the inorder traversal of the tree. Examples: Input: N = 3  Output: 5 6 2 7 3 1 4Input: N = 3  Output: 2 3 5 1 4 6 Approach: The inorder traversal of an N-ary tree is defined as visiting all the children except the last then the root and finall 6 min read Construct a Perfect Binary Tree from Preorder Traversal Given an array pre[], representing the Preorder traversal of a Perfect Binary Tree consisting of N nodes, the task is to construct a Perfect Binary Tree from the given Preorder Traversal and return the root of the tree. Examples: Input: pre[] = {1, 2, 4, 5, 3, 6, 7}Output: 1 / \ / \ 2 3 / \ / \ / \ 11 min read Morris traversal for Postorder Perform Postorder Tree traversal using Morris Traversal.Examples:Input: 1 / \ 2 3 / \ / \ 6 7 8 9Output: 6 7 2 8 9 3 1Input: 5 / \ 2 3 / \ / \ 4 N 8 9Output: 4 2 8 9 3 5 Approach: The approach to performing Morris Traversal for Postorder is similar to Morris traversal for Preorder except swapping be 8 min read Like