Open In App

Check if given Preorder, Inorder and Postorder traversals are of same tree

Last Updated : 15 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given Preorder, Inorder, and Postorder traversals of some tree. Write a program to check if they all are of the same tree. 

Examples: 

Input: Inorder -> 4 2 5 1 3
Preorder -> 1 2 4 5 3
Postorder -> 4 5 2 3 1
Output: Yes
Explanation: All of the above three traversals are of the same tree
1
/ \
2 3
/ \
4 5
Input: Inorder -> 4 2 5 1 3
Preorder -> 1 5 4 2 3
Postorder -> 4 1 2 3 5
Output: No

The most basic approach to solve this problem will be to first construct a tree using two of the three given traversals and then do the third traversal on this constructed tree and compare it with the given traversal. If both of the traversals are the same then print Yes otherwise print No. Here, we use Inorder and Preorder traversals to construct the tree. We may also use Inorder and Postorder traversal instead of Preorder traversal for tree construction. You may refer to this post on how to construct a tree from given Inorder and Preorder traversal. After constructing the tree, we will obtain the Postorder traversal of this tree and compare it with the given Postorder traversal.

Below is the implementation of the above approach: 

C++
/* C++ program to check if all three given
   traversals are of the same tree */
#include <bits/stdc++.h>
using namespace std;

// A Binary Tree Node
struct Node
{
    int data;
    struct Node *left, *right;
};

// Utility function to create a new tree node
Node* newNode(int data)
{
    Node *temp = new Node;
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}

/* Function to find index of value in arr[start...end]
   The function assumes that value is present in in[] */
int search(int arr[], int strt, int end, int value)
{
    for (int i = strt; i <= end; i++)
    {
        if(arr[i] == value)
            return i;
    }
}

/* Recursive function to construct binary tree 
   of size len from Inorder traversal in[] and 
   Preorder traversal pre[].  Initial values
   of inStrt and inEnd should be 0 and len -1.  
   The function doesn't do any error checking for 
   cases where inorder and preorder do not form a 
   tree */
Node* buildTree(int in[], int pre[], int inStrt, 
                                      int inEnd)
{
    static int preIndex = 0;
 
    if(inStrt > inEnd)
        return NULL;
 
    /* Pick current node from Preorder traversal 
       using preIndex and increment preIndex */
    Node *tNode = newNode(pre[preIndex++]);
 
    /* If this node has no children then return */
    if (inStrt == inEnd)
        return tNode;
 
    /* Else find the index of this node in 
       Inorder traversal */
    int inIndex = search(in, inStrt, inEnd, tNode->data);
 
    /* Using index in Inorder traversal, 
       construct left and right subtress */
    tNode->left = buildTree(in, pre, inStrt, inIndex-1);
    tNode->right = buildTree(in, pre, inIndex+1, inEnd);
 
    return tNode;
}

/* function to compare Postorder traversal 
   on constructed tree and given Postorder */
int checkPostorder(Node* node, int postOrder[], int index)
{
    if (node == NULL)
        return index;
 
    /* first recur on left child */
    index = checkPostorder(node->left,postOrder,index);
     
    /* now recur on right child */
    index = checkPostorder(node->right,postOrder,index);    
  
    /* Compare if data at current index in 
       both Postorder traversals are same */
    if (node->data == postOrder[index])
        index++;
    else
        return -1;

    return index;
}

// Driver program to test above functions
int main()
{
    int inOrder[] = {4, 2, 5, 1, 3};
    int preOrder[] = {1, 2, 4, 5, 3};
    int postOrder[] = {4, 5, 2, 3, 1};

    int len = sizeof(inOrder)/sizeof(inOrder[0]);

    // build tree from given 
    // Inorder and Preorder traversals
    Node *root = buildTree(inOrder, preOrder, 0, len - 1);

    // compare postorder traversal on constructed
    // tree with given Postorder traversal
    int index = checkPostorder(root,postOrder,0);

    // If both postorder traversals are same 
    if (index == len)
        cout << "Yes";
    else
        cout << "No";

    return 0;
}
Java
/* Java program to check if all three given 
traversals are of the same tree */
import java.util.*;
class GfG {
    static int preIndex = 0;

// A Binary Tree Node 
static class Node 
{ 
    int data; 
    Node left, right; 
}

// Utility function to create a new tree node 
static Node newNode(int data) 
{ 
    Node temp = new Node(); 
    temp.data = data; 
    temp.left = null;
    temp.right = null; 
    return temp; 
} 

/* Function to find index of value in arr[start...end] 
The function assumes that value is present in in[] */
static int search(int arr[], int strt, int end, int value) 
{ 
    for (int i = strt; i <= end; i++) 
    { 
        if(arr[i] == value) 
            return i; 
    } 
    return -1;
} 

/* Recursive function to construct binary tree 
of size len from Inorder traversal in[] and 
Preorder traversal pre[]. Initial values 
of inStrt and inEnd should be 0 and len -1. 
The function doesn't do any error checking for 
cases where inorder and preorder do not form a 
tree */
static Node buildTree(int in[], int pre[], int inStrt, int inEnd) 
{ 

    if(inStrt > inEnd) 
        return null; 

    /* Pick current node from Preorder traversal 
    using preIndex and increment preIndex */
    Node tNode = newNode(pre[preIndex++]); 

    /* If this node has no children then return */
    if (inStrt == inEnd) 
        return tNode; 

    /* Else find the index of this node in 
    Inorder traversal */
    int inIndex = search(in, inStrt, inEnd, tNode.data); 

    /* Using index in Inorder traversal, 
    construct left and right subtress */
    tNode.left = buildTree(in, pre, inStrt, inIndex-1); 
    tNode.right = buildTree(in, pre, inIndex+1, inEnd); 

    return tNode; 
} 

/* function to compare Postorder traversal 
on constructed tree and given Postorder */
static int checkPostorder(Node node, int postOrder[], int index) 
{ 
    if (node == null) 
        return index; 

    /* first recur on left child */
    index = checkPostorder(node.left,postOrder,index); 
    if(index == -1) return index;
    /* now recur on right child */
    index = checkPostorder(node.right,postOrder,index);     
    if(index == -1) return index;
    /* Compare if data at current index in 
    both Postorder traversals are same */
    if (node.data == postOrder[index]) 
        index++; 
    else
        return -1; 

    return index; 
} 

// Driver program to test above functions 
public static void main(String[] args) 
{ 
    int inOrder[] = {4, 2, 5, 1, 3}; 
    int preOrder[] = {1, 2, 4, 5, 3}; 
    int postOrder[] = {4, 5, 2, 3, 1}; 

    int len = inOrder.length; 

    // build tree from given 
    // Inorder and Preorder traversals 
    Node root = buildTree(inOrder, preOrder, 0, len - 1); 

    // compare postorder traversal on constructed 
    // tree with given Postorder traversal 
    int index = checkPostorder(root,postOrder,0); 

    // If both postorder traversals are same 
    if (index == len) 
        System.out.println("Yes"); 
    else
        System.out.println("No"); 

}
} 
Python3
# Python3 program to check if
# all three given traversals 
# are of the same tree 
class node:
  
    def __init__(self, x):
      
        self.data = x
        self.left = None
        self.right = None

preIndex = 0

# Function to find index of value 
# in arr[start...end]. The function 
# assumes that value is present in in
def search(arr, strt, end, value):
  
    for i in range(strt, end + 1):
        if(arr[i] == value):
            return i

# Recursive function to construct
# binary tree of size lenn from 
# Inorder traversal in and Preorder 
# traversal pre[].  Initial values 
# of inStrt and inEnd should be 0 
# and lenn -1. The function doesn't 
# do any error checking for cases 
# where inorder and preorder do not 
# form a tree 
def buildTree(inn, pre, inStrt, inEnd):
  
    global preIndex

    if(inStrt > inEnd):
        return None

    # Pick current node from Preorder 
    # traversal using preIndex and 
    # increment preIndex 
    tNode = node(pre[preIndex])
    preIndex += 1

    # If this node has no children 
    # then return
    if (inStrt == inEnd):
        return tNode

    # Else find the index of this 
    # node in Inorder traversal
    inIndex = search(inn, inStrt, 
                     inEnd, tNode.data)

    # Using index in Inorder traversal, 
    # construct left and right subtress
    tNode.left = buildTree(inn, pre, inStrt, 
                           inIndex - 1)
    tNode.right = buildTree(inn, pre, 
                            inIndex + 1, inEnd)

    return tNode

# function to compare Postorder traversal
# on constructed tree and given Postorder
def checkPostorder(node, postOrder, index):
    if (node == None):
        return index

    # first recur on left child
    index = checkPostorder(node.left,
                           postOrder, 
                           index)

    # now recur on right child
    index = checkPostorder(node.right,
                           postOrder,
                           index)

    # Compare if data at current index in
    # both Postorder traversals are same
    if (node.data == postOrder[index]):
        index += 1 
    else:
        return - 1

    return index

# Driver code
if __name__ == '__main__':
  
    inOrder = [4, 2, 5, 1, 3]
    preOrder = [1, 2, 4, 5, 3]
    postOrder = [4, 5, 2, 3, 1]
    lenn = len(inOrder)

    # build tree from given
    # Inorder and Preorder traversals
    root = buildTree(inOrder, preOrder, 
                     0, lenn - 1)

    # compare postorder traversal on 
    # constructed tree with given 
    # Postorder traversal
    index = checkPostorder(root, postOrder, 0)

    # If both postorder traversals are same
    if (index == lenn):
        print("Yes")
    else:
        print("No")

# This code is contributed by Mohit Kumar 29
C#
/* C# program to check if all three given 
traversals are of the same tree */
using System;
    
public class GfG 
{
    static int preIndex = 0;

    // A Binary Tree Node 
    class Node 
    { 
        public int data; 
        public Node left, right; 
    }

    // Utility function to create a new tree node 
    static Node newNode(int data) 
    { 
        Node temp = new Node(); 
        temp.data = data; 
        temp.left = null;
        temp.right = null; 
        return temp; 
    } 

    /* Function to find index of 
    value in arr[start...end] 
    The function assumes that 
    value is present in in[] */
    static int search(int []arr, int strt,
                        int end, int value) 
    { 
        for (int i = strt; i <= end; i++) 
        { 
            if(arr[i] == value) 
                return i; 
        } 
        return -1;
    } 

    /* Recursive function to construct 
    binary tree of size len from Inorder
    traversal in[] and Preorder traversal
    pre[]. Initial values of inStrt and 
    inEnd should be 0 and len -1. The 
    function doesn't do any error checking for 
    cases where inorder and preorder do not form a 
    tree */
    static Node buildTree(int []In, int []pre,
                            int inStrt, int inEnd) 
    { 

        if(inStrt > inEnd) 
            return null; 

        /* Pick current node from Preorder traversal 
        using preIndex and increment preIndex */
        Node tNode = newNode(pre[preIndex++]); 

        /* If this node has no children then return */
        if (inStrt == inEnd) 
            return tNode; 

        /* Else find the index of this node in 
        Inorder traversal */
        int inIndex = search(In, inStrt, inEnd, tNode.data); 

        /* Using index in Inorder traversal, 
        construct left and right subtress */
        tNode.left = buildTree(In, pre, inStrt, inIndex - 1); 
        tNode.right = buildTree(In, pre, inIndex + 1, inEnd); 

        return tNode; 
    } 

    /* function to compare Postorder traversal 
    on constructed tree and given Postorder */
    static int checkPostorder(Node node, int []postOrder, int index) 
    { 
        if (node == null) 
            return index; 

        /* first recur on left child */
        index = checkPostorder(node.left,postOrder,index); 

        /* now recur on right child */
        index = checkPostorder(node.right,postOrder,index);     

        /* Compare if data at current index in 
        both Postorder traversals are same */
        if (node.data == postOrder[index]) 
            index++; 
        else
            return -1; 

        return index; 
    } 

    // Driver code 
    public static void Main() 
    { 
        int []inOrder = {4, 2, 5, 1, 3}; 
        int []preOrder = {1, 2, 4, 5, 3}; 
        int []postOrder = {4, 5, 2, 3, 1}; 

        int len = inOrder.Length; 

        // build tree from given 
        // Inorder and Preorder traversals 
        Node root = buildTree(inOrder, preOrder, 0, len - 1); 

        // compare postorder traversal on constructed 
        // tree with given Postorder traversal 
        int index = checkPostorder(root, postOrder, 0); 

        // If both postorder traversals are same 
        if (index == len) 
            Console.WriteLine("Yes"); 
        else
            Console.WriteLine("No"); 

    }
}

/* This code is contributed PrinciRaj1992 */
Javascript
<script>
/* Javascript program to check if all three given
traversals are of the same tree */
    
    let preIndex = 0;
    // A Binary Tree Node
    class Node
    {
        // Utility function to create a new tree node
        constructor(data)
        {
            this.data=data;
            this.left=this.right=null;
        }
    }
    
/* Function to find index of value in arr[start...end]
The function assumes that value is present in in[] */    
function search(arr,strt,end,value)
{
    for (let i = strt; i <= end; i++)
    {
        if(arr[i] == value)
            return i;
    }
    return -1;
}

/* Recursive function to construct binary tree
of size len from Inorder traversal in[] and
Preorder traversal pre[]. Initial values
of inStrt and inEnd should be 0 and len -1.
The function doesn't do any error checking for
cases where inorder and preorder do not form a
tree */
function buildTree(In,pre,inStrt,inEnd)
{
    if(inStrt > inEnd)
        return null;
 
    /* Pick current node from Preorder traversal
    using preIndex and increment preIndex */
    let tNode = new Node(pre[preIndex++]);
 
    /* If this node has no children then return */
    if (inStrt == inEnd)
        return tNode;
 
    /* Else find the index of this node in
    Inorder traversal */
    let inIndex = search(In, inStrt, inEnd, tNode.data);
 
    /* Using index in Inorder traversal,
    construct left and right subtress */
    tNode.left = buildTree(In, pre, inStrt, inIndex-1);
    tNode.right = buildTree(In, pre, inIndex+1, inEnd);
 
    return tNode;
}

/* function to compare Postorder traversal
on constructed tree and given Postorder */
function checkPostorder(node,postOrder,index)
{
    if (node == null)
        return index;
 
    /* first recur on left child */
    index = checkPostorder(node.left,postOrder,index);
     
    /* now recur on right child */
    index = checkPostorder(node.right,postOrder,index);    
     
    /* Compare if data at current index in
    both Postorder traversals are same */
    if (node.data == postOrder[index])
        index++;
    else
        return -1;
 
    return index;
}

// Driver program to test above functions
let inOrder=[4, 2, 5, 1, 3];
let preOrder=[1, 2, 4, 5, 3];
let postOrder=[4, 5, 2, 3, 1];

let len = inOrder.length;
 
    // build tree from given
    // Inorder and Preorder traversals
    let root = buildTree(inOrder, preOrder, 0, len - 1);
 
    // compare postorder traversal on constructed
    // tree with given Postorder traversal
    let index = checkPostorder(root,postOrder,0);
 
    // If both postorder traversals are same
    if (index == len)
        document.write("Yes");
    else
        document.write("No");
    

// This code is contributed by patel2127
</script>

Output
Yes

Time Complexity : O( n * n ), where n is number of nodes in the tree.
Space Complexity: O(n), for call stack

Efficient algorithm using hash map to store indices of inorder elements :

While building the tree from Inorder and Preorder traversal we need to check if the inorder and preorder traversals are valid themself for some tree, and if yes , then keep building the tree, but if valid binary tree can not be built from given inorder and preorder traversal, then we must stop building the tree and return false. And also we can build the tree from inorder and preorder traversal in O(n) time using hashmap to store the indices of the inorder elements’ array.

Implementation:

C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
    int data;
    Node *left, *right;

    Node(int val)
    {
        data = val;
        left = right = NULL;
    }
};
Node* buildTreeFromInorderPreorder(
    int inStart, int inEnd, int& preIndex, int preorder[],
    unordered_map<int, int>& inorderIndexMap,
    bool& notPossible)
{
    if (inStart > inEnd)
        return NULL;

    // build the current Node
    int rootData = preorder[preIndex];
    Node* root = new Node(rootData);
    preIndex++;

    // find the node in inorderIndexMap
    if (inorderIndexMap.find(rootData)
        == inorderIndexMap.end()) {
        notPossible = true;
        return root;
    }

    int inorderIndex = inorderIndexMap[rootData];
    if (!(inStart <= inorderIndex
          && inorderIndex <= inEnd)) {
        notPossible = true;
        return root;
    }

    int leftInorderStart = inStart,
        leftInorderEnd = inorderIndex - 1,
        rightInorderStart = inorderIndex + 1,
        rightInorderEnd = inEnd;

    root->left = buildTreeFromInorderPreorder(
        leftInorderStart, leftInorderEnd, preIndex,
        preorder, inorderIndexMap, notPossible);

    if (notPossible)
        return root;

    root->right = buildTreeFromInorderPreorder(
        rightInorderStart, rightInorderEnd, preIndex,
        preorder, inorderIndexMap, notPossible);

    return root;
}

bool checkPostorderCorrect(Node* root, int& postIndex,
                           int postorder[])
{
    if (!root)
        return true;

    if (!checkPostorderCorrect(root->left, postIndex,
                               postorder))
        return false;
    if (!checkPostorderCorrect(root->right, postIndex,
                               postorder))
        return false;

    return (root->data == postorder[postIndex++]);
}

void printPostorder(Node* root)
{
    if (!root)
        return;

    printPostorder(root->left);
    printPostorder(root->right);

    cout << root->data << ", ";
}

void printInorder(Node* root)
{
    if (!root)
        return;

    printInorder(root->left);
    cout << root->data << ", ";
    printInorder(root->right);
}

bool checktree(int preorder[], int inorder[],
               int postorder[], int N)
{
    // Your code goes here
    if (N == 0)
        return true;

    unordered_map<int, int> inorderIndexMap;
    for (int i = 0; i < N; i++)
        inorderIndexMap[inorder[i]] = i;

    int preIndex = 0;

    // return checkInorderPreorder(0, N - 1, preIndex,
    // preorder, inorderIndexMap) &&
    // checkInorderPostorder(0, N - 1, postIndex, postorder,
    // inorderIndexMap);

    bool notPossible = false;

    Node* root = buildTreeFromInorderPreorder(
        0, N - 1, preIndex, preorder, inorderIndexMap,
        notPossible);

    if (notPossible)
        return false;

    int postIndex = 0;

    return checkPostorderCorrect(root, postIndex,
                                 postorder);
}

// Driver program to test above functions
int main()
{
    int inOrder[] = { 4, 2, 5, 1, 3 };
    int preOrder[] = { 1, 2, 4, 5, 3 };
    int postOrder[] = { 4, 5, 2, 3, 1 };

    int len = sizeof(inOrder) / sizeof(inOrder[0]);

    // If both postorder traversals are same
    if (checktree(preOrder, inOrder, postOrder, len))
        cout << "Yes";
    else
        cout << "No";

    return 0;
}
Java
// Java code for the above approach
import java.util.*;

class Node {
  int data;
  Node left, right;

  Node(int val)
  {
    data = val;
    left = right = null;
  }
}

class Main {
  static Node buildTreeFromInorderPreorder(
    int inStart, int inEnd, int[] preorder,
    Map<Integer, Integer> inorderIndexMap,
    boolean[] notPossible, int preIndex)
  {

    if (inStart > inEnd)
      return null;

    // build the current Node
    int rootData = preorder[preIndex];
    Node root = new Node(rootData);
    preIndex++;

    // find the node in inorderIndexMap
    if (!inorderIndexMap.containsKey(rootData)) {
      notPossible[0] = true;
      return root;
    }
    int inorderIndex = inorderIndexMap.get(rootData);
    if (!(inStart <= inorderIndex
          && inorderIndex <= inEnd)) {
      notPossible[0] = true;
      return root;
    }

    int leftInorderStart = inStart,
    leftInorderEnd = inorderIndex - 1,
    rightInorderStart = inorderIndex + 1,
    rightInorderEnd = inEnd;

    root.left = buildTreeFromInorderPreorder(
      leftInorderStart, leftInorderEnd, preorder,
      inorderIndexMap, notPossible, preIndex);

    if (notPossible[0])
      return root;

    root.right = buildTreeFromInorderPreorder(
      rightInorderStart, rightInorderEnd, preorder,
      inorderIndexMap, notPossible, preIndex);

    return root;
  }

  static boolean checkPostorderCorrect(Node root,
                                       int[] postorder,
                                       int postIndex)
  {
    if (root == null)
      return true;

    if (!checkPostorderCorrect(root.left, postorder,
                               postIndex))
      return false;
    if (!checkPostorderCorrect(root.right, postorder,
                               postIndex))
      return false;

    return (root.data == postorder[postIndex++]);
  }

  static boolean checktree(int[] preorder, int[] inorder,
                           int[] postorder, int N)
  {
    // Your code goes here
    if (N == 0)
      return true;

    Map<Integer, Integer> inorderIndexMap
      = new HashMap<>();
    for (int i = 0; i < N; i++)
      inorderIndexMap.put(inorder[i], i);

    int preIndex = 0;
    boolean[] notPossible = new boolean[] { false };

    Node root = buildTreeFromInorderPreorder(
      0, N - 1, preorder, inorderIndexMap,
      notPossible, preIndex);

    if (notPossible[0])
      return true;

    int postIndex = 0;

    return checkPostorderCorrect(root, postorder,
                                 postIndex);
  }

  public static void main(String[] args)
  {
    int inOrder[] = { 4, 2, 5, 1, 3 };
    int preOrder[] = { 1, 2, 4, 5, 3 };
    int postOrder[] = { 4, 5, 2, 3, 1 };

    int len = inOrder.length;

    if (checktree(preOrder, inOrder, postOrder, len))
      System.out.println("The tree is valid");
    else
      System.out.println("The tree is not valid");
  }
}

// This code is contributed by lokeshpotta20.
Python3
# Python3 program for the above approach
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None


preIndex = 0
notPossible = False


def buildTreeFromInorderPreorder(inStart, inEnd, preorder, inorderIndexMap):
    if(inStart > inEnd):
        return None

    # build the current node
    global preIndex
    global notPossible
    rootData = preorder[preIndex]
    root = Node(rootData)
    preIndex += 1

    # find the node in inorderIndexMap
    if(inorderIndexMap.get(rootData) == None):
        notPossible = True
        return root

    inorderIndex = inorderIndexMap.get(rootData)
    if((inStart <= inorderIndex and inorderIndex <= inEnd) == False):
        notPossible = True
        return root

    leftInorderStart = inStart
    leftInorderEnd = inorderIndex - 1
    rightInorderStart = inorderIndex + 1
    rightInroderEnd = inEnd

    root.left = buildTreeFromInorderPreorder(
        leftInorderStart, leftInorderEnd, preorder, inorderIndexMap)

    if(notPossible == True):
        return root

    root.right = buildTreeFromInorderPreorder(
        rightInorderStart, rightInroderEnd, preorder, inorderIndexMap)

    return root


postIndex = 0


def checkPostorderCorrect(root, postOrder):
    if(root == None):
        return True
    if(checkPostorderCorrect(root.left, postOrder) == False):
        return False

    if(checkPostorderCorrect(root.right, postOrder) == False):
        return False

    global postIndex
    if(root.data == postOrder[postIndex]):
        postIndex += 1
        return True
    else:
        postIndex += 1
        return False


def printPostorder(root):
    if(root == None):
        return

    printPostorder(root.left)
    printPostorder(root.right)
    print(root.data)


def printInorder(root):
    if(root == None):
        return

    printInorder(root.left)
    print(root.data)
    printInorder(root.right)


def checktree(preorder, inorder, postorder, N):
    if(N == 0):
        return True
    inorderIndexMap = {}
    for i in range(N):
        inorderIndexMap[inorder[i]] = i

    root = buildTreeFromInorderPreorder(0, N-1, preorder, inorderIndexMap)

    global notPossible
    if(notPossible == True):
        return False

    if(checkPostorderCorrect(root, postorder)):
        return True
    else:
        return False


# driver program
inOrder = [4, 2, 5, 1, 3]
preOrder = [1, 2, 4, 5, 3]
postOrder = [4, 5, 2, 3, 1]

len = len(inOrder)

# if both postorder traversal as same
if(checktree(preOrder, inOrder, postOrder, len) == True):
    print("Yes")
else:
    print("No")

# THIS CODE IS CONTRIBUTED BY KIRTI AGARWAL(KIRTIAGARWAL23121999)
C#
using System;
using System.Collections.Generic;

class Node
{
    public int data;
    public Node left;
    public Node right;

    public Node(int val)
    {
        data = val;
        left = right = null;
    }
}

class Program
{
    static Node BuildTreeFromInorderPreorder(int inStart, int inEnd, int[] preorder, Dictionary<int, int> inorderIndexMap, ref bool notPossible, ref int preIndex)
    {
        if (inStart > inEnd)
            return null;

        // Build the current node
        int rootData = preorder[preIndex];
        Node root = new Node(rootData);
        preIndex++;

        // Find the node in inorderIndexMap
        if (!inorderIndexMap.ContainsKey(rootData))
        {
            notPossible = true;
            return root;
        }

        int inorderIndex = inorderIndexMap[rootData];
        if (!(inStart <= inorderIndex && inorderIndex <= inEnd))
        {
            notPossible = true;
            return root;
        }

        int leftInorderStart = inStart;
        int leftInorderEnd = inorderIndex - 1;
        int rightInorderStart = inorderIndex + 1;
        int rightInorderEnd = inEnd;

        root.left = BuildTreeFromInorderPreorder(leftInorderStart, leftInorderEnd, preorder, inorderIndexMap, ref notPossible, ref preIndex);

        if (notPossible)
            return root;

        root.right = BuildTreeFromInorderPreorder(rightInorderStart, rightInorderEnd, preorder, inorderIndexMap, ref notPossible, ref preIndex);

        return root;
    }

    static bool CheckPostorderCorrect(Node root, int[] postorder, ref int postIndex)
    {
        if (root == null)
            return true;

        if (!CheckPostorderCorrect(root.left, postorder, ref postIndex))
            return false;

        if (!CheckPostorderCorrect(root.right, postorder, ref postIndex))
            return false;

        return (root.data == postorder[postIndex++]);
    }

    static bool CheckTree(int[] preorder, int[] inorder, int[] postorder, int N)
    {
        if (N == 0)
            return true;

        Dictionary<int, int> inorderIndexMap = new Dictionary<int, int>();
        for (int i = 0; i < N; i++)
            inorderIndexMap.Add(inorder[i], i);

        int preIndex = 0;
        bool notPossible = false;

        Node root = BuildTreeFromInorderPreorder(0, N - 1, preorder, inorderIndexMap, ref notPossible, ref preIndex);

        if (notPossible)
            return false;

        int postIndex = 0;

        return CheckPostorderCorrect(root, postorder, ref postIndex);
    }

     static void Main(string[] args)
  {
    int[] inOrder = { 4, 2, 5, 1, 3 };
    int[] preOrder = { 1, 2, 4, 5, 3 };
    int[] postOrder = { 4, 5, 2, 3, 1 };

    int len = inOrder.Length;

    if (CheckTree(preOrder, inOrder, postOrder, len))
      Console.WriteLine("Yes");
    else
      Console.WriteLine("No");
  }
}
Javascript
// JavaScript program for the above approach
class Node{
    constructor(val){
        this.data = val;
        this.left = null;
        this.right =  null;
    }
}

let preIndex = 0;
let notPossible = false;
function buildTreeFromInorderPreorder(inStart, inEnd, 
preorder, inorderIndexMap){
    if(inStart > inEnd){
        return null;
    }
    
    // build the current node
    let rootData = preorder[preIndex];
    let root = new Node(rootData);
    preIndex++;
    
    // find the node in inorderIndexMap
    if(inorderIndexMap.has(rootData) == false){
        notPossible = true;
        return root;
    }
    
    let inorderIndex = inorderIndexMap.get(rootData);
    if(!(inStart <= inorderIndex && inorderIndex <= inEnd)){
        notPosstible = true;
        return root;
    }
    
    let leftInorderStart = inStart;
    let leftInorderEnd = inorderIndex - 1;
    let rightInorderStart = inorderIndex + 1;
    let rightInorderEnd = inEnd;
        
    root.left = buildTreeFromInorderPreorder(
        leftInorderStart, leftInorderEnd, preorder, inorderIndexMap);
        
    if(notPossible == true)
        return root;
    
    root.right = buildTreeFromInorderPreorder(
        leftInorderStart, leftInorderEnd, preorder, inorderIndexMap);
    
    return root;
}

let postIndex = 0;
function checkPostorderCorrect(root, postOrder){
    if(root == null) return true;
    if(!checkPostorderCorrect(root.left, postOrder))
        return false;
    
    if(!checkPostorderCorrect(root.right, postOrder))
        return false;
        
    return (root.data == postOrder[postIndex++]);
}

function printPostorder(root){
    if(root == null) return;
    
    printPostorder(root.left);
    printPostorder(root.right);
    document.write(root.data + " ");
}

function printInorder(root){
    if(root == null) return;
    printInorder(root.left);
    document.write(root.data + " ");
    printInorder(root.right);
}

function checktree(preorder, inorder, postorder, N){
    if(N == 0) return true;
    
    inorderIndexMap = new Map();
    for(let i = 0; i<N; i++){
        inorderIndexMap.set(inorder[i], i);
    }
    
    let root = buildTreeFromInorderPreorder(0, N-1, 
    preorder, inorderIndexMap);
    
    if(notPossible == true) return false;
    
    return checkPostorderCorrect(root, postorder);
}

// driver program
let inOrder = [4, 2, 5, 1, 3];
let preOrder = [1, 2, 4, 5, 3];
let postOrder = [4, 5, 2, 3, 1];

let len = inOrder.length;

// If both postorder traversals as same
if(checktree(preOrder, inOrder, postOrder, len))
    document.write("Yes");
else
    document.write("No");
    
// This code is contributed  by Yash Agarwal(yashagarwal2852002)

Output
Yes

Time Complexity: O(N)
Auxiliary Space: O(N), where N is number of nodes in the tree.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads