Preorder from Inorder and Postorder traversals (original) (raw)
Last Updated : 11 Jul, 2025
Given the **Inorder and **Postorder traversals of a **binary tree, the task is to find the **Preorder traversal.
**Examples:
**Input: post[] = [4, 5, 2, 6, 3, 1]
in[] = [4, 2, 5, 1, 3, 6]
**Output: 1 2 4 5 3 6
**Explanation: Traversals in the above example represents following tree:
The idea is to first construct the binary tree from a given postorder and inorder, then use a simple **recursive method to print the **preorder traversal of the constructed tree.
**Note: We can optimize the above method. We do not need to create the tree.
Table of Content
- [Naive approach] Search current element every time - O(n^2) Time and O(n) Space
- [Expected approach] Using hashing - O(n) Time and O(n) Space
[Naive approach] Search current element every time - O(n^2) Time and O(n) Space
The idea is that **root is always the **first item in **preorder traversal and it must be the last item in **postorder traversal. We first push **right subtree to a **stack, then left subtree, and finally, we push root. Finally, we print contents of **stack. To find boundaries of **left and right subtrees in **post[] and in[], we search root in **in[], all elements before root in **in[] are elements of **left subtree, and all elements after root are elements of right subtree. In post[], all elements after index of root in **in[] are elements of right subtree. And elements before index (including the element at index and excluding the first element) are elements of left subtree.
Below is the implementation of the above approach:
C++ `
// C++ program to print Postorder traversal from given // Inorder and Preorder traversals.
#include<bits/stdc++.h> using namespace std;
// A utility function to search data in in[] int search(vector& in, int data, int n) { int i = 0; for (i = 0; i < n; i++) if (in[i] == data) return i; return i; }
// Fills preorder traversal of tree with given // inorder and postorder traversals in a stack void fillPre(vector& in, vector& post, int inStrt, int inEnd, stack &s, int n, int &postIndex) { if (inStrt > inEnd) return;
// Find index of next item in postorder traversal in
// inorder.
int val = post[postIndex];
int inIndex = search(in, val, n);
postIndex--;
// traverse right tree
fillPre(in, post, inIndex + 1, inEnd, s, n, postIndex);
// traverse left tree
fillPre(in, post, inStrt, inIndex - 1, s, n, postIndex);
s.push(val);}
// This function basically initializes postIndex // as last element index, then fills stack with // reverse preorder traversal using printPre vector getPreorderTraversal(vector& in, vector& post, int n) { int len = n; int postIndex = len - 1; stack s; fillPre(in, post, 0, len - 1, s, n, postIndex); vector result; while (s.size() > 0) { result.push_back(s.top()); s.pop(); } return result; }
int main() { vector in = { 4, 2, 5, 1, 3, 6 }; vector post = { 4, 5, 2, 6, 3, 1 }; int n = in.size(); vector result = getPreorderTraversal(in, post, n); for(int i : result) { cout << i << " "; } }
Java
// Java program to print Preorder traversal from given // Inorder and Postorder traversals.
import java.util.*;
class Node { int data; Node left, right;
Node(int data) {
this.data = data;
this.left = this.right = null;
}}
class GfG {
// A utility function to search data in in[]
static int search(List<Integer> in, int data, int n) {
for (int i = 0; i < n; i++)
if (in.get(i) == data)
return i;
return -1;
}
// Fills preorder traversal of tree with given
// inorder and postorder traversals in a stack
static void fillPre(List<Integer> in, List<Integer> post, int inStrt,
int inEnd, Stack<Integer> s, int n, int[] postIndex) {
if (inStrt > inEnd)
return;
// Find index of next item in postorder
// traversal in inorder.
int val = post.get(postIndex[0]);
int inIndex = search(in, val, n);
postIndex[0]--;
// traverse right tree
fillPre(in, post, inIndex + 1, inEnd, s, n, postIndex);
// traverse left tree
fillPre(in, post, inStrt, inIndex - 1, s, n, postIndex);
s.push(val);
}
// This function basically initializes postIndex as last element index,
// then fills stack with reverse preorder traversal using fillPre
static List<Integer> getPreorderTraversal
(List<Integer> in, List<Integer> post, int n) {
int postIndex = n - 1;
Stack<Integer> s = new Stack<>();
fillPre(in, post, 0, n - 1, s, n, new int[]{postIndex});
List<Integer> result = new ArrayList<>();
while (!s.isEmpty()) {
result.add(s.pop());
}
return result;
}
public static void main(String[] args) {
List<Integer> in = Arrays.asList(4, 2, 5, 1, 3, 6);
List<Integer> post = Arrays.asList(4, 5, 2, 6, 3, 1);
int n = in.size();
List<Integer> result = getPreorderTraversal(in, post, n);
for (int i : result) {
System.out.print(i + " ");
}
}}
Python
Python program to print Preorder traversal from given
Inorder and Postorder traversals.
A utility function to search data in in[]
def search(inorder, data, n): for i in range(n): if inorder[i] == data: return i return -1
Fills preorder traversal of tree with given
inorder and postorder traversals in a stack
def fill_pre(inorder, postorder, in_start, in_end,
stack, n, post_index):
if in_start > in_end:
return
# Find index of next item in postorder traversal in inorder.
val = postorder[post_index[0]]
in_index = search(inorder, val, n)
post_index[0] -= 1
# Traverse right tree
fill_pre(inorder, postorder, in_index + 1, in_end,\
stack, n, post_index)
# Traverse left tree
fill_pre(inorder, postorder, in_start, in_index - 1,\
stack, n, post_index)
stack.append(val)This function initializes postIndex as
last element index and fills stack
with reverse preorder traversal using fillPre
def get_preorder_traversal(inorder, postorder, n):
post_index = [n - 1]
stack = []
fill_pre(inorder, postorder, 0, n - 1, stack, n,
post_index)
return list(reversed(stack))if name == "main":
inorder = [4, 2, 5, 1, 3, 6]
postorder = [4, 5, 2, 6, 3, 1]
n = len(inorder)
result = get_preorder_traversal(inorder, postorder, n)
for val in result:
print(val, end=" ")C#
// C# program to print Preorder traversal from given // Inorder and Postorder traversals.
using System; using System.Collections.Generic;
class Node { public int data; public Node left, right;
public Node(int data) {
this.data = data;
this.left = this.right = null;
}}
class GfG {
// A utility function to search data in in[]
static int Search(List<int> inorder, int data, int n) {
for (int i = 0; i < n; i++) {
if (inorder[i] == data)
return i;
}
return -1;
}
// Fills preorder traversal of tree with given
// inorder and postorder traversals in a stack
static void FillPre(List<int> inorder, List<int> postorder, int inStart,
int inEnd, Stack<int> s, int n, ref int postIndex) {
if (inStart > inEnd)
return;
// Find index of next item in postorder
// traversal in inorder.
int val = postorder[postIndex];
int inIndex = Search(inorder, val, n);
postIndex--;
// Traverse right tree
FillPre(inorder, postorder, inIndex + 1, inEnd,
s, n, ref postIndex);
// Traverse left tree
FillPre(inorder, postorder, inStart, inIndex - 1, s,
n, ref postIndex);
s.Push(val);
}
// This function initializes postIndex as last
// element index and fills stack
// with reverse preorder traversal using FillPre
static List<int> GetPreorderTraversal(List<int> inorder,
List<int> postorder, int n) {
int postIndex = n - 1;
Stack<int> s = new Stack<int>();
FillPre(inorder, postorder, 0, n - 1, s, n, ref postIndex);
List<int> result = new List<int>();
while (s.Count > 0) {
result.Add(s.Pop());
}
return result;
}
static void Main(string[] args) {
List<int> inorder = new List<int> { 4, 2, 5, 1, 3, 6 };
List<int> postorder = new List<int> { 4, 5, 2, 6, 3, 1 };
int n = inorder.Count;
List<int> result = GetPreorderTraversal
(inorder, postorder, n);
foreach (int val in result) {
Console.Write(val + " ");
}
}}
JavaScript
// JavaScript program to print Preorder traversal from given // Inorder and Postorder traversals.
// A utility function to search data in inorder[] function search(inorder, data, n) { for (let i = 0; i < n; i++) { if (inorder[i] === data) return i; } return -1; }
// Fills preorder traversal of tree with given // inorder and postorder traversals in a stack function fillPre(inorder, postorder, inStart, inEnd, stack, n, postIndex) { if (inStart > inEnd) return;
// Find index of next item in postorder
// traversal in inorder.
const val = postorder[postIndex[0]];
const inIndex = search(inorder, val, n);
postIndex[0]--;
// Traverse right tree
fillPre(inorder, postorder, inIndex + 1, inEnd,
stack, n, postIndex);
// Traverse left tree
fillPre(inorder, postorder, inStart, inIndex - 1,
stack, n, postIndex);
stack.push(val);}
// This function initializes postIndex as last // element index and fills stack // with reverse preorder traversal using fillPre function getPreorderTraversal(inorder, postorder, n) { let postIndex = [n - 1]; const stack = []; fillPre(inorder, postorder, 0, n - 1, stack, n, postIndex); return stack.reverse(); }
const inorder = [4, 2, 5, 1, 3, 6]; const postorder = [4, 5, 2, 6, 3, 1]; const n = inorder.length;
const result = getPreorderTraversal (inorder, postorder, n); console.log(result.join(" "));
`
[Expected approach] Using hashing - O(n) Time and O(n) Space
The idea is to use **hashing for finding the element so we do not need to call **search function every time. By doing this we can reduce time complexity to O(n). The approach of this method is same as above but here we use hashing.
Below is the implementation of the above approach:
C++ `
// C++ program to print Preorder traversal from // given Inorder and Postorder traversals.
#include <bits/stdc++.h> using namespace std;
// Fills preorder traversal of tree with given // inorder and postorder traversals in a vector void fillPre(vector &in, vector &post, int inStrt, int inEnd, vector &result, unordered_map<int, int> &mp, int &postIndex) { if (inStrt > inEnd) return;
// Find index of next item in postorder
// traversal in inorder
int val = post[postIndex];
int inIndex = mp[val];
postIndex--;
// Traverse right subtree
fillPre(in, post, inIndex + 1, inEnd,
result, mp, postIndex);
// Traverse left subtree
fillPre(in, post, inStrt, inIndex - 1,
result, mp, postIndex);
// Store the current value in result
// (preorder traversal)
result.push_back(val);}
// This function initializes postIndex as the last element index, // fills the result vector with preorder traversal, and returns it. vector getPreorderTraversal(vector &in, vector &post, int n) { int postIndex = n - 1; vector result;
// Store inorder element positions
// in an unordered map
unordered_map<int, int> mp;
for (int i = 0; i < n; i++)
mp[in[i]] = i;
// Fill the preorder traversal in
// the result vector
fillPre(in, post, 0, n - 1, result, mp, postIndex);
reverse(result.begin(), result.end());
return result;}
int main() {
vector<int> in = {4, 2, 5, 1, 3, 6};
vector<int> post = {4, 5, 2, 6, 3, 1};
int n = in.size();
vector<int> preorder = getPreorderTraversal(in, post, n);
for (int val : preorder) {
cout << val << " ";
}
return 0;}
Java
// Java program to print Preorder traversal from // given Inorder and Postorder traversals.
import java.util.*;
class Node { int val; Node left, right;
Node(int val) {
this.val = val;
left = right = null;
}}
class GfG {
// Fills preorder traversal of tree with given
// inorder and postorder traversals in a list
static void fillPre(List<Integer> in,
List<Integer> post, int inStrt,
int inEnd, List<Integer> result,
Map<Integer, Integer> mp,
int[] postIndex) {
if (inStrt > inEnd)
return;
// Find index of next item in postorder
//traversal in inorder
int val = post.get(postIndex[0]);
int inIndex = mp.get(val);
postIndex[0]--;
// Traverse right subtree
fillPre(in, post, inIndex + 1, inEnd, result, mp,
postIndex);
// Traverse left subtree
fillPre(in, post, inStrt, inIndex - 1, result, mp,
postIndex);
// Store the current value in result (preorder
// traversal)
result.add(val);
}
// This function initializes postIndex as the last
// element index, fills the result list with preorder
// traversal, and returns it.
static List<Integer>
getPreorderTraversal(List<Integer> in,
List<Integer> post, int N) {
int[] postIndex = { N - 1 };
List<Integer> result = new ArrayList<>();
// Store inorder element positions
// in an unordered map
Map<Integer, Integer> mp = new HashMap<>();
for (int i = 0; i < N; i++)
mp.put(in.get(i), i);
// Fill the preorder traversal in
// the result list
fillPre(in, post, 0, N - 1, result, mp, postIndex);
Collections.reverse(result);
return result;
}
public static void main(String[] args) {
List<Integer> in = Arrays.asList(4, 2, 5, 1, 3, 6);
List<Integer> post
= Arrays.asList(4, 5, 2, 6, 3, 1);
int N = in.size();
List<Integer> preorder
= getPreorderTraversal(in, post, N);
for (int val : preorder) {
System.out.print(val + " ");
}
}}
Python
Python program to print Preorder traversal from
given Inorder and Postorder traversals.
def fill_pre(in_order, post_order, in_start, in_end,
result, mp, post_index):
if in_start > in_end:
return
# Find index of next item in postorder
# traversal in inorder
val = post_order[post_index[0]]
in_index = mp[val]
post_index[0] -= 1
# Traverse right subtree
fill_pre(in_order, post_order, in_index +
1, in_end, result, mp, post_index)
# Traverse left subtree
fill_pre(in_order, post_order, in_start,
in_index - 1, result, mp, post_index)
# Store the current value in result (preorder traversal)
result.append(val)This function initializes post_index as the last element index,
fills the result list with preorder traversal, and returns it.
def get_preorder_traversal(in_order, post_order, N): post_index = [N - 1] result = []
# Store inorder element positions
# in a hashmap
mp = {val: idx for idx, val in enumerate(in_order)}
# Fill the preorder traversal in
# the result list
fill_pre(in_order, post_order, 0, N - 1,\
result, mp, post_index)
result.reverse()
return resultif name == 'main': in_order = [4, 2, 5, 1, 3, 6] post_order = [4, 5, 2, 6, 3, 1] N = len(in_order)
preorder = get_preorder_traversal \
(in_order, post_order, N)
print(' '.join(map(str, preorder)))C#
// C# program to print Preorder traversal from // given Inorder and Postorder traversals.
using System; using System.Collections.Generic;
class Node { public int val; public Node left, right;
public Node(int val)
{
this.val = val;
left = right = null;
}}
class GfG {
// Fills preorder traversal of tree with given
// inorder and postorder traversals in a list
static void fillPre(List<int> inOrder,
List<int> postOrder, int inStrt,
int inEnd, List<int> result,
Dictionary<int, int> mp,
ref int postIndex) {
if (inStrt > inEnd)
return;
// Find index of next item in postorder traversal in
// inorder
int val = postOrder[postIndex];
int inIndex = mp[val];
postIndex--;
// Traverse right subtree
fillPre(inOrder, postOrder, inIndex + 1, inEnd,
result, mp, ref postIndex);
// Traverse left subtree
fillPre(inOrder, postOrder, inStrt, inIndex - 1,
result, mp, ref postIndex);
// Store the current value in result (preorder
// traversal)
result.Add(val);
}
// This function initializes postIndex as the last
// element index, fills the result list with preorder
// traversal, and returns it.
static List<int>
GetPreorderTraversal(List<int> inOrder,
List<int> postOrder, int N) {
int postIndex = N - 1;
List<int> result = new List<int>();
// Store inorder element positions
// in a dictionary
Dictionary<int, int> mp
= new Dictionary<int, int>();
for (int i = 0; i < N; i++)
mp[inOrder[i]] = i;
// Fill the preorder traversal in
// the result list
fillPre(inOrder, postOrder, 0, N - 1, result, mp,
ref postIndex);
result.Reverse();
return result;
}
static void Main() {
List<int> inOrder
= new List<int>{ 4, 2, 5, 1, 3, 6 };
List<int> postOrder
= new List<int>{ 4, 5, 2, 6, 3, 1 };
int N = inOrder.Count;
List<int> preorder
= GetPreorderTraversal(inOrder, postOrder, N);
foreach(int val in preorder) {
Console.Write(val + " ");
}
}}
JavaScript
// JavaScript program to print Preorder traversal from // given Inorder and Postorder traversals.
function fillPre(inOrder, postOrder, inStrt, inEnd, result, mp, postIndex) { if (inStrt > inEnd) return;
// Find index of next item in postorder traversal in
// inorder
let val = postOrder[postIndex[0]];
let inIndex = mp[val];
postIndex[0]--;
// Traverse right subtree
fillPre(inOrder, postOrder, inIndex + 1, inEnd, result,
mp, postIndex);
// Traverse left subtree
fillPre(inOrder, postOrder, inStrt, inIndex - 1, result,
mp, postIndex);
// Store the current value in result (preorder
// traversal)
result.push(val);}
// This function initializes postIndex as the last element // index, fills the result array with preorder traversal, // and returns it. function getPreorderTraversal(inOrder, postOrder, N) { let postIndex = [ N - 1 ]; let result = [];
// Store inorder element
// positions in a hashmap
let mp = {};
for (let i = 0; i < N; i++) {
mp[inOrder[i]] = i;
}
// Fill the preorder traversal in the result array
fillPre(inOrder, postOrder, 0, N - 1, result, mp,
postIndex);
result.reverse();
return result;}
let inOrder = [ 4, 2, 5, 1, 3, 6 ]; let postOrder = [ 4, 5, 2, 6, 3, 1 ]; let N = inOrder.length; let preorder = getPreorderTraversal(inOrder, postOrder, N); console.log(preorder.join(" "));
`
