• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

March 25, 2014 by Dhaval Dave

Generate Postorder traversal of Tree from Inorder and Preorder traversal of tree without generating Tree.

Input:
In-order traversal in[] = {4, 2, 5, 1, 3, 6}
Pre-order traversal pre[] = {1, 2, 4, 5, 3, 6}

Output:
Post-order traversal is {4, 5, 2, 6, 3, 1}

         1
       /  \    
      2    3
    /  \    \
   4    5    6

We can print post-order traversal without constructing the tree. 

  1. Root is always the first item in preorder traversal and it must be the last item in postorder traversal.
    – Here take 1.
  2. We first recursively print left subtree, then recursively print right subtree.
    – From In-Order take Left nodes of root as left subtree and right nodes as right subtree
    – eg here 4,2,5 as Left subtree and 3,6 Right subtree of 1.
    – Check Pre-order here U can understand 2 is root of left subtree and and at left of 2 all nodes will be at left subtree and right of 2 are right subtree of 2.
    they are 4, and 5.
  3. Print left subtree first.
  4. Same traverse for right subtree of 1 and u’ll find 3 as root of right subtree and 6 as right subtree of 3.
  5. print this right subtree.

Code

#include <iostream>using namespace std;
int search(int arr[], int x, int n)
{
    for (int i = 0; i < n; i++)  if (arr[i] == x)  return i;  return -1; }
 
void printPostOrder(int in[], int pre[], int n)
{
   int root = search(in, pre[0], n);

   if (root != 0)
     printPostOrder(in, pre+1, root);

   if (root != n-1)
      printPostOrder(in+root+1, pre+root+1, n-root-1);
  
 cout << pre[0] << " ";
}


Similar Articles

Filed Under: Adobe Interview Questions, Interview Questions, Microsoft Interview Questions, problem Tagged With: Binary Tree, tree

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Hackerearth : Counting Subarrays

Find the element that appears once others appears thrice

SAP Interview Questions

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

C Program for TAIL command of UNIX

Find Nearest Minimum number in left side in O(n)

VMWare SDEII Interview

Maximum difference between two elements s.t larger element appears after the smaller number

Given a float number convert it into the string WITHOUT using any inbuilt Function

Implement a generic binary search algorithm for Integer Double String etc

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

Print Power Set of a Set

Binary Tree Isomorphic to each other

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

Trapping Rain Water

Skiing on Mountains Matrix

Find position of the only set bit

Find min element in Sorted Rotated Array (With Duplicates)

Client Server C program

Add Sub Multiply very large number stored as string

Find next greater number with same set of digits

K’th Largest Element in BST when modification to BST is not allowed

strtok()

Find the number ABCD such that when multipled by 4 gives DCBA.

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

C++ OOPs Part1

Given a sorted array and a number x, find the pair in array whose sum is closest to x

FizzBuzz Solution C C++

Given Set of words or A String find whether chain is possible from these words or not

Copyright © 2025 · Genesis Framework · WordPress · Log in