Postorder Traversal | Practice | GeeksforGeeks

Given a binary tree, find the Postorder Traversal of it.
For Example, the postorder traversal of the following tree is: 
5 10 39 1
        1
     /     \
   10     39
  /
5

Example 1:
Input:
        19
     /     \
    10      8
  /    \
 11    13
Output: 11 13 10 8 19

Example 2:
Input:
          11
         /
       15
      /
     7
Output: 7 15 11


Your Task:
You don't need to read input or print anything. Your task is to complete the function postOrder() that takes root node as input and returns an array containing the postorder traversal of the given Binary Tree.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).

Constraints:
1 <= Number of nodes <= 105
0 <= Data of a node <= 106
 
0
superrhitik4582 days ago
vector<int> vi;void post(Node* root){    if(root!=NULL){        post(root->left); post(root->right); vi.push_back(root->data);    }     return;}vector <int> postOrder(Node* root){ // Your code herepost(root); vector<int> res=vi;vi.clear();return res;}
0
itachinamikaze2211 week ago
ArrayList<Integer> postOrder(Node root)   {       Stack<Node> st= new Stack<Node>();       ArrayList<Integer> res= new ArrayList<>();      Node curr=root;      Node temp;      while(curr!=null||st.isEmpty()==false)      {          if(curr!=null)          {              st.push(curr);              curr=curr.left;          }          else          {              temp=st.peek().right;              if(temp==null)              {                  temp=st.peek();                  st.pop();                  res.add(temp.data);                  while(!st.isEmpty() && temp==st.peek().right)                  {                      temp=st.peek();                      st.pop();                      res.add(temp.data);                  }              }              else              curr= temp;          }      }      return res;   }
0
rajpateriya3 weeks ago
  JAVA solution
  
  void post(Node root,  ArrayList<Integer> res)
    {
        if(root == null)
        {
            return;
        }
        post(root.left,res);
        post(root.right,res);
        res.add(root.data);
    }
    
    ArrayList<Integer> postOrder(Node root)
    {
       ArrayList<Integer> res=new  ArrayList<>();
       post(root, res);
       return res;
    }
0
amankumar2781 month ago
Java Solution...(Very easy): 
class Tree{   //Function to return a list containing the postorder traversal of the tree.   ArrayList<Integer> postOrder(Node root)   {      // Your code goes here      ArrayList<Integer> list=new ArrayList<>();      if(root==null){          return list;      }      list.addAll(postOrder(root.left));      list.addAll(postOrder(root.right));      list.add(root.data);      return list;   }}
0
adarshgupta4011 month ago
Java recursive code -
all test cases passed
class Tree
{
    ArrayList<Integer> postOrder(Node root)
    {
       //left- right- root
       ArrayList<Integer> res = new ArrayList<>();
       
       if( root == null)
        return res;
       
       res.addAll(postOrder(root.left));
       res.addAll(postOrder(root.right));
       res.add(root.data);
       
       return res;
    }
}
0
amiransarimy1 month ago
Python Solutions
 
#Function to return a list containing the postorder traversal of the tree.
def postOrder(root):
   if root is None:
       return [] # return empty list if root is None
   resList = []
   postOrderHelper(root,resList)
   return resList
def postOrderHelper(root,resList):
   if root is None:
       return
   postOrderHelper(root.left,resList)
   postOrderHelper(root.right, resList)
   resList.append(root.data)
-2
vs5094111 month ago
 
Why this code is giving wrong output?
 
 
vector<int> v;vector <int> postOrder(Node* root){ if(root==NULL)return v; postOrder(root->left); postOrder(root->right); v.push_back(root->data); return v;}
 
 
+1
iamnobodyji2 months ago
ArrayList<Integer> postOrder(Node root)
   {
      // Your code goes here
      ArrayList<Integer> arr = new ArrayList<Integer>();
      if(root==null) return arr;
      arr.addAll(postOrder(root.left));
      arr.addAll(postOrder(root.right));
      arr.add(root.data);
      return arr;
   }
+2
mridulbhaskarabc2 months ago
#Contributed By: Mridul Bhaskar
def postOrder(root):
    if(root is None):
        return []
    else:
        return postOrder(root.left) + postOrder(root.right)+[root.data]
 
+2
vishalja77192 months ago
C++ 0.01 faster 
 void postorder(Node* root,vector<int> &v)
 {
     if(root == NULL)
        return;
    
    postorder(root->left,v);
    postorder(root->right,v);
    v.push_back(root->data);
 }

vector <int> postOrder(Node* root)
{
  // Your code here
  vector<int>res;
  postorder(root,res);
  return res;
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.