Find postorder traversal of BST from preorder traversal
Last Updated :
22 Aug, 2022
Given an array representing preorder traversal of BST, print its postorder traversal.
Examples:
Input : 40 30 35 80 100
Output : 35 30 100 80 40
Input : 40 30 32 35 80 90 100 120
Output : 35 32 30 120 100 90 80 40
Prerequisite: Construct BST from given preorder traversal
Simple Approach: A simple solution is to first construct BST from a given preorder traversal as described in this post. After constructing the tree, perform postorder traversal on it.
Efficient Approach:
An efficient approach is to find postorder traversal without constructing the tree. The idea is to traverse the given preorder array and maintain a range in which current element should lie. This is to ensure that the BST property is always satisfied. Initially the range is set to {minval = INT_MIN, maxval = INT_MAX}. In preorder traversal, the first element is always the root, and it will certainly lie in the initial range.
So store the first element of the preorder array. In postorder traversal, first left and right subtrees are printed and then root data is printed. So first recursive call for left and right subtrees are performed and then the value of root is printed. For left subtree range is updated to {minval, root->data} and for right subtree range is updated to {root->data, maxval}. If the current preorder array element does not lie in the range specified for it, then it does not belong to a current subtree, return from recursive calls until the correct position of that element is not found.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findPostOrderUtil( int pre[], int n, int minval,
int maxval, int & preIndex)
{
if (preIndex == n)
return ;
if (pre[preIndex] < minval || pre[preIndex] > maxval) {
return ;
}
int val = pre[preIndex];
preIndex++;
findPostOrderUtil(pre, n, minval, val, preIndex);
findPostOrderUtil(pre, n, val, maxval, preIndex);
cout << val << " " ;
}
void findPostOrder( int pre[], int n)
{
int preIndex = 0;
findPostOrderUtil(pre, n, INT_MIN, INT_MAX, preIndex);
}
int main()
{
int pre[] = { 40, 30, 35, 80, 100 };
int n = sizeof (pre) / sizeof (pre[0]);
findPostOrder(pre, n);
return 0;
}
|
Java
import java.util.*;
class Solution {
static class INT {
int data;
INT( int d) { data = d; }
}
static void findPostOrderUtil( int pre[], int n,
int minval, int maxval,
INT preIndex)
{
if (preIndex.data == n)
return ;
if (pre[preIndex.data] < minval
|| pre[preIndex.data] > maxval) {
return ;
}
int val = pre[preIndex.data];
preIndex.data++;
findPostOrderUtil(pre, n, minval, val, preIndex);
findPostOrderUtil(pre, n, val, maxval, preIndex);
System.out.print(val + " " );
}
static void findPostOrder( int pre[], int n)
{
INT preIndex = new INT( 0 );
findPostOrderUtil(pre, n, Integer.MIN_VALUE,
Integer.MAX_VALUE, preIndex);
}
public static void main(String args[])
{
int pre[] = { 40 , 30 , 35 , 80 , 100 };
int n = pre.length;
findPostOrder(pre, n);
}
}
|
Python3
INT_MIN = - 2 * * 31
INT_MAX = 2 * * 31
def findPostOrderUtil(pre, n, minval,
maxval, preIndex):
if (preIndex[ 0 ] = = n):
return
if (pre[preIndex[ 0 ]] < minval or
pre[preIndex[ 0 ]] > maxval):
return
val = pre[preIndex[ 0 ]]
preIndex[ 0 ] + = 1
findPostOrderUtil(pre, n, minval,
val, preIndex)
findPostOrderUtil(pre, n, val,
maxval, preIndex)
print (val, end = " " )
def findPostOrder(pre, n):
preIndex = [ 0 ]
findPostOrderUtil(pre, n, INT_MIN,
INT_MAX, preIndex)
if __name__ = = '__main__' :
pre = [ 40 , 30 , 35 , 80 , 100 ]
n = len (pre)
findPostOrder(pre, n)
|
C#
using System;
class GFG {
public class INT {
public int data;
public INT( int d) { data = d; }
}
public static void findPostOrderUtil( int [] pre, int n,
int minval,
int maxval,
INT preIndex)
{
if (preIndex.data == n) {
return ;
}
if (pre[preIndex.data] < minval
|| pre[preIndex.data] > maxval) {
return ;
}
int val = pre[preIndex.data];
preIndex.data++;
findPostOrderUtil(pre, n, minval, val, preIndex);
findPostOrderUtil(pre, n, val, maxval, preIndex);
Console.Write(val + " " );
}
public static void findPostOrder( int [] pre, int n)
{
INT preIndex = new INT(0);
findPostOrderUtil(pre, n, int .MinValue,
int .MaxValue, preIndex);
}
public static void Main( string [] args)
{
int [] pre = new int [] { 40, 30, 35, 80, 100 };
int n = pre.Length;
findPostOrder(pre, n);
}
}
|
Javascript
<script>
class INT
{
constructor(d)
{
this .data=d;
}
}
function findPostOrderUtil(pre,n,minval,maxval,preIndex)
{
if (preIndex.data == n)
return ;
if (pre[preIndex.data] < minval
|| pre[preIndex.data] > maxval) {
return ;
}
let val = pre[preIndex.data];
preIndex.data++;
findPostOrderUtil(pre, n, minval, val, preIndex);
findPostOrderUtil(pre, n, val, maxval, preIndex);
document.write(val + " " );
}
function findPostOrder(pre,n)
{
let preIndex = new INT(0);
findPostOrderUtil(pre, n, Number.MIN_VALUE,
Number.MAX_VALUE, preIndex);
}
let pre=[40, 30, 35, 80, 100];
let n = pre.length;
findPostOrder(pre, n);
</script>
|
Complexity Analysis:
- Time Complexity: O(N), where N is the number of nodes.
- Auxiliary Space: O(N) (Function call stack size).
Please Login to comment...