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:
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:
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.
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>usingnamespacestd;// Node StructureclassNode{public:intdata;Node*left;Node*right;Node(intv){data=v;left=right=nullptr;}};voidpostOrder(Node*node,vector<int>&res){if(node==nullptr)return;// First we traverse left subtreepostOrder(node->left,res);// After visiting left, traverse right subtreepostOrder(node->right,res);// now we visit noderes.push_back(node->data);}intmain(){//Represent Tree// 1// / \ // 2 3// / \ \ // 4 5 6Node*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(4);root->left->right=newNode(5);root->right->right=newNode(6);vector<int>result;postOrder(root,result);// Print the postorderfor(intval:result)cout<<val<<" ";return0;}
C
#include<stdio.h>#include<stdlib.h>// Node StructurestructNode{intdata;structNode*left;structNode*right;};structNode*createNode(intv){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=v;node->left=NULL;node->right=NULL;returnnode;}voidpostOrder(structNode*node){if(node==NULL)return;// First we traverse left subtreepostOrder(node->left);// After visiting left, traverse right subtreepostOrder(node->right);// now we visit nodeprintf("%d ",node->data);}intmain(){//Represent Tree// 1// / \ // 2 3// / \ \ // 4 5 6structNode*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);return0;}
Java
importjava.util.ArrayList;// Node StructureclassNode{intdata;Nodeleft;Noderight;Node(intv){data=v;left=null;right=null;}}publicclassGFG{staticvoidpostOrder(Nodenode,ArrayList<Integer>res){if(node==null)return;// First we traverse left subtreepostOrder(node.left,res);// After visiting left, traverse right subtreepostOrder(node.right,res);// now we visit noderes.add(node.data);}publicstaticvoidmain(String[]args){//Represent Tree// 1// / \// 2 3// / \ \// 4 5 6Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.right=newNode(6);ArrayList<Integer>result=newArrayList<>();postOrder(root,result);// Print the postorderfor(intval:result)System.out.print(val+" ");}}
Python
# Node StructureclassNode:def__init__(self,v):self.data=vself.left=Noneself.right=NonedefpostOrder(node,res):ifnodeisNone:return# First we traverse left subtreepostOrder(node.left,res)# After visiting left, traverse right subtreepostOrder(node.right,res)# now we visit noderes.append(node.data)if__name__=="__main__":#Represent Tree# 1# / \# 2 3# / \ \# 4 5 6root=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 postorderforvalinresult:print(val,end=" ")
C#
usingSystem;usingSystem.Collections.Generic;// Node StructureclassNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intv){data=v;left=null;right=null;}}classGFG{staticvoidpostOrder(Nodenode,List<int>res){if(node==null)return;// First we traverse left subtreepostOrder(node.left,res);// After visiting left, traverse right subtreepostOrder(node.right,res);// now we visit noderes.Add(node.data);}staticvoidMain(){//Represent Tree// 1// / \// 2 3// / \ \// 4 5 6Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.right=newNode(6);List<int>result=newList<int>();postOrder(root,result);// Print the postorderforeach(intvalinresult)Console.Write(val+" ");}}
JavaScript
// Node StructureclassNode{constructor(v){this.data=v;this.left=null;this.right=null;}}functionpostOrder(node,res){if(node===null)return;// First we traverse left subtreepostOrder(node.left,res);// After visiting left, traverse right subtreepostOrder(node.right,res);// now we visit noderes.push(node.data);}// Driver code//Represent Tree// 1// / \// 2 3// / \ \// 4 5 6letroot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.right=newNode(6);letresult=[];postOrder(root,result);// Print the postorderfor(letvalofresult)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:
It is used for tree deletion because subtrees are deleted before the current node.