Check if two given key sequences construct same BSTs

Given two arrays which represent two sequences of keys that are used to create BSTs. Imagine we make a Binary Search Tree (BST) from each array. We need to tell whether two BSTs will be identical or not without actually constructing the tree.
Examples:

Let the input arrays be a[] and b[]

Example 1:
a[] = {2, 4, 1, 3} will construct following tree.
   2
 /  \
1    4
    /
   3
b[] = {2, 4, 3, 1} will also also construct the same tree.
   2
 /  \
1    4
    /
   3 
So the output is "True"

Example 2:
a[] = {8, 3, 6, 1, 4, 7, 10, 14, 13}
b[] = {8, 10, 14, 3, 6, 4, 1, 7, 13}

They both construct the same following BST, so output is "True"
            8
         /    \
       3       10
     /  \        \
    1     6       14
        /   \     /
       4     7   13  

1) Compare sizes of two arrays. If not same, return false.
2) Compare first values of two arrays. If not same, return false.
3) Create two lists from each given array such that the first list contains values smaller than first item of the corresponding array. And second list contains greater values.
4) Recursively check the first list of first array with first list of second and same for second list.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if given two arrays represent
// same BST.
import java.util.ArrayList;
import java.util.Arrays;
  
public class SameBST {
    static boolean sameBSTs(ArrayList<Integer> aL1,
                            ArrayList<Integer> aL2)
    {
        // Base cases
        if (aL1.size() != aL2.size())
            return false;
        if (aL1.size() == 0)
            return true;
        if (aL1.get(0) != aL2.get(0))
            return false;
  
        // Construct two lists from each input array. The first
        // list contains values smaller than first value, i.e.,
        // left subtree. And second list contains right subtree.
        ArrayList<Integer> aLLeft1 = new ArrayList<Integer>();
        ArrayList<Integer> aLRight1 = new ArrayList<Integer>();
        ArrayList<Integer> aLLeft2 = new ArrayList<Integer>();
        ArrayList<Integer> aLRight2 = new ArrayList<Integer>();
        for (int i = 1; i < aL1.size(); i++) {
            if (aL1.get(i) < aL1.get(0))
                aLLeft1.add(aL1.get(i));
            else
                aLRight1.add(aL1.get(i));
  
            if (aL2.get(i) < aL2.get(0))
                aLLeft2.add(aL2.get(i));
            else
                aLRight2.add(aL2.get(i));
        }
  
        // Recursively compare left and right
        // subtrees.
        return sameBSTs(aLLeft1, aLLeft2) &&
               sameBSTs(aLRight1, aLRight2);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        ArrayList<Integer> aL1 =
                         new ArrayList<Integer>(Arrays.
                           asList(3, 5, 4, 6, 1, 0, 2));
        ArrayList<Integer> aL2 = 
                         new ArrayList<Integer>(Arrays.
                          asList(3, 1, 5, 2, 4, 6, 0));
  
        System.out.println(sameBSTs(aL1, aL2));
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if given 
// two arrays represent same BST.
using System;
using System.Linq;
using System.Collections.Generic;
  
public class SameBST 
{
    static bool sameBSTs(List<int> aL1,
                            List<int> aL2)
    {
        // Base cases
        if (aL1.Count != aL2.Count)
            return false;
        if (aL1.Count == 0)
            return true;
        if (aL1[0] != aL2[0])
            return false;
  
        // Construct two lists from each 
        // input array. The first list contains 
        // values smaller than first value, i.e.,
        // left subtree. And second list contains right subtree.
        List<int> aLLeft1 = new List<int>();
        List<int> aLRight1 = new List<int>();
        List<int> aLLeft2 = new List<int>();
        List<int> aLRight2 = new List<int>();
        for (int i = 1; i < aL1.Count; i++) 
        {
            if (aL1[i] < aL1[0])
                aLLeft1.Add(aL1[i]);
            else
                aLRight1.Add(aL1[i]);
  
            if (aL2[i] < aL2[0])
                aLLeft2.Add(aL2[i]);
            else
                aLRight2.Add(aL2[i]);
        }
  
        // Recursively compare left and right
        // subtrees.
        return sameBSTs(aLLeft1, aLLeft2) &&
            sameBSTs(aLRight1, aLRight2);
    }
  
    // Driver code
    public static void Main()
    {
        int []arr1 = {3, 5, 4, 6, 1, 0, 2};
        List<int> aL1 = arr1.ToList();
        int []arr2 = {3, 1, 5, 2, 4, 6, 0};
        List<int> aL2 = arr2.ToList();
  
        Console.WriteLine(sameBSTs(aL1, aL2));
    }
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


Output:

true

Time Complexity : O(n * n)

Please refer below post for O(n) solution
Check for Identical BSTs without building the trees



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : princiraj1992