Preorder to Postorder Traversal

Learn via video course
FREE
View all courses
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
by Jitender Punia
1000
4.9
Start Learning
Topics Covered

Problem Statement

Given an array that represents the preorder traversal of a binary search tree, write a program to find the postorder traversal of the BST.

Example

The preorder of a BST is given below: preorder[] = {10, 5, 8, 25, 47} The postorder for the example will be: postorder[] = {8, 5, 47, 25, 10}

Example Explanation

BST example

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Input/Output

Input An array of size N represents the preorder traversal of a BST.

Output The postorder traversal of the given BST.

Constraints

1<=N<=1031 <= N <= 10^{3}
1<=arr[i]<=1041 <= arr[i] <= 10^{4}

Algorithm 1 - Simple Approach

  • Step 1 - Construct a Binary Search Tree from the given preorder traversal using the function 'constructBST'.
  • Step 2 - Perform postorder traversal on the constructed BST.

Code Implementation

C++

Java

Python

Output

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Time Complexity

Time Complexity to construct BST is O(N2N^2). Time complexity to find out the postorder traversal of the constructed BST is O(N) where N is no of the nodes in a binary tree. Thus, the overall time complexity of converting preorder to postorder traversal using this approach will be O(N2N^2).

Space Complexity

The space complexity of this approach is O(N) where N is the extra space required for the set visited.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Algorithm 2 - Efficient Approach

In this approach of converting preorder to postorder traversal construction of a tree is avoided.

  • The preorder array is traversed to maintain a range in which the current element should lie.
  • In preorder traversal, the first element or root is stored as it lies in the initial range.
  • Next, recursive calls are made for the left and right subtrees respectively. Then, the value of the root is printed.
    • For left subtree range is minval to root->data.
    • For right subtree range is root->data to maxval.
    • If the element considered lies outside the range specified, then it does not belong to a current subtree. Such recursive calls are returned until the correct position of that element is not found.

BST approach 2 example

Code Implementation

C++

Java

Python

Output

Time Complexity

Since every node is traversed once, the time complexity for this approach is O(N), where N is the number of nodes.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Space Complexity

Due to the use of a call stack, the time complexity of this approach is O(N).

Algorithm 3 - Using Iteration and Recursion

The conversion of preorder to postorder traversal can also be done using iteration and recursion.

  • Step 1 - Preorder can be represented as root -> left -> right and postOrder can be represented as left -> right -> root.
  • Step 2 - A loop is used, and the last element of the left group is taken as the pivot element. The pivot is the point from which the array will be rotated. It is found by finding the index of the smallest element greater than the root.
  • Step 3 - Consider the left section as elements from index 1 to index before pivot.
  • Step 4 - Consider the right section as elements in between the last index and pivot.
  • Step 5 - Repeat step 4 recursively till both the left and right sections have at most 1 element. Print the only element in the section.
  • Step 6 - Finally, print the element at index 0.

BST approach 3 example

Code Implementation

C++

Java

Python

Output

Time Complexity

For the execution of this approach, the array consisting of the preorder traversal elements is traversed to find the pivot in O(N). The rotation also takes O(N) time causing the overall time complexity for the Preorder function to be O(N). However, since the function is called recursively the overall time complexity will be O(N2N^2).

Space Complexity

The Preorder function is called recursively which creates a new array every time it is called. Hence, the overall space complexity for this approach will be O(N2N^2).

Conclusion

  • The traversal in which at first the root node is visited, followed by the left sub-tree, and after that right sub-tree is visited is called preorder traversal.
  • The traversal in which at first the left sub-tree is visited, followed by the right sub-tree, and finally the root is called postorder traversal.
  • Approach 1 - In this approach of conversion of preorder to postorder traversal, a BST is created.
    • Time Complexity - O(N2N^2)
    • Space Complexity - O(n)
  • Approach 2 - Traversing the preorder array with a limit on values to separate values of the left and right subtree.
    • Time Complexity - O(n)
    • Space Complexity - O(n)
  • Approach 3 - Using a loop to calculate the pivot element where preorder is root -> left -> right and postorder is left -> right -> root. In this approach iteration and recursion are used.
    • Time Complexity - O(N2N^2)
    • Space Complexity - O(N2N^2)

FAQ

Q. What is Preorder Traversal?

A. The process of visiting the root node first, followed by the left sub-tree and finally, the right sub-tree is known as preorder traversal. It can be represented as - rootleftrightroot → left → right

Q. What is Postorder Traversal?

A. The process of visiting the left sub-tree and followed by the right sub-tree and finally the root is known as postorder traversal. It can be represented as - leftrightrootleft → right → root