Open In App

Check if two BSTs contain same set of elements

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given two Binary Search Trees consisting of unique positive elements, we have to check whether the two BSTs contain the same set of elements or not. 

Note: The structure of the two given BSTs can be different. 

For example, 
 

9 (5)

The above two BSTs contains same set of elements {5, 10, 12, 15, 20, 25}

Method 1: The most simple method will be to traverse first tree and store its element in a list or array. Now, traverse 2nd tree and simultaneously check if the current element is present in the list or not. If yes, then mark the element in the list as negative and check for further elements otherwise if no, then immediately terminate the traversal and print No. If all the elements of 2nd tree is present in the list and are marked negative then finally traverse the list to check if there are any non-negative elements left. If Yes then it means that the first tree had some extra element otherwise both trees consist the same set of elements.

Time Complexity: O( n * n ) , where n is the number of nodes in the BST. 
Auxiliary Space: O( n ).

Method 2: This method is an optimization of the above approach. If we observe carefully, we will see that in the above approach, searching for elements in the list takes linear time. We can optimize this operation to be done in constant time using a hashmap instead of a list. We insert elements of both trees in different hash sets. Finally, we compare if both hash sets contain the same elements or not.

Below is the complete implementation of the above approach: 

C++




// CPP program to check if two BSTs contains
// same set of elements
#include<bits/stdc++.h>
using namespace std;
  
// BST Node
struct Node
{
    int data;
    struct Node* left;
    struct Node* right;
};
  
// Utility function to create new Node
Node* newNode(int val)
{
    Node* temp = new Node;
    temp->data = val;
    temp->left = temp->right = NULL; 
    return temp;
}
  
// function to insert elements of the 
// tree to map m
void insertToHash(Node* root, unordered_set<int> &s)
{
    if (!root)
        return;
    insertToHash(root->left, s);
    s.insert(root->data);
    insertToHash(root->right, s);
}
  
// function to check if the two BSTs contain
// same set  of elements
bool checkBSTs(Node* root1, Node* root2)
    // Base cases 
    if (!root1 && !root2) 
        return true;
    if ((root1 && !root2) || (!root1 && root2))
        return false;
          
    // Create two hash sets and store 
    // elements both BSTs in them.
    unordered_set<int> s1, s2;
    insertToHash(root1, s1);
    insertToHash(root2, s2);
  
    // Return true if both hash sets 
    // contain same elements.
    return (s1 == s2);
}
  
// Driver program to check above functions
int main()
    // First BST
    Node* root1 = newNode(15);
    root1->left = newNode(10);
    root1->right = newNode(20);
    root1->left->left = newNode(5);
    root1->left->right = newNode(12);
    root1->right->right = newNode(25);
      
    // Second BST
    Node* root2 = newNode(15);
    root2->left = newNode(12);
    root2->right = newNode(20);
    root2->left->left = newNode(5);
    root2->left->left->right = newNode(10);
    root2->right->right = newNode(25);
      
    // check if two BSTs have same set of elements
    if (checkBSTs(root1, root2))
        cout << "YES";
    else
        cout << "NO";
    return 0;        
}


Java




// Java program to check if two BSTs
// contain same set of elements
import java.util.*;
  
class GFG {
  
    // BST Node
    static class Node {
        int data;
        Node left;
        Node right;
    };
  
    // Utility function to create new Node
    static Node newNode(int val)
    {
        Node temp = new Node();
        temp.data = val;
        temp.left = temp.right = null;
        return temp;
    }
  
    // function to insert elements
    // of the tree to map m
    static void storeInorder(Node root, Vector<Integer> v)
    {
        if (root == null)
            return;
        storeInorder(root.left, v);
        v.add(root.data);
        storeInorder(root.right, v);
    }
  
    // function to check if the two BSTs
    // contain same set of elements
    static boolean checkBSTs(Node root1, Node root2)
    {
        // Base cases
        if (root1 == null && root2 == null)
            return true;
        if ((root1 == null && root2 != null)
            || (root1 != null && root2 == null))
            return false;
  
        // Create two vectors and store
        // inorder traversals of both BSTs
        // in them.
        Vector<Integer> v1 = new Vector<Integer>();
        Vector<Integer> v2 = new Vector<Integer>();
        storeInorder(root1, v1);
        storeInorder(root2, v2);
  
        // Return true if both vectors are
        // identical
        return (v1.hashCode() == v2.hashCode());
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // First BST
        Node root1 = newNode(15);
        root1.left = newNode(10);
        root1.right = newNode(20);
        root1.left.left = newNode(5);
        root1.left.right = newNode(12);
        root1.right.right = newNode(25);
  
        // Second BST
        Node root2 = newNode(15);
        root2.left = newNode(12);
        root2.right = newNode(20);
        root2.left.left = newNode(5);
        root2.left.left.right = newNode(10);
        root2.right.right = newNode(25);
  
        // check if two BSTs have same set of elements
        if (checkBSTs(root1, root2))
            System.out.print("YES");
        else
            System.out.print("NO");
    }
}
  
// This code is contributed by Rajput-Ji
// Code corrected by Prithi_Raj


Python3




# Python3 program to check if two BSTs contains
# same set of elements
  
# BST Node
class Node:
    def __init__(self):
        self.data = 0
        self.left = None
        self.right = None
  
# Utility function to create Node
def Node_(val1):
  
    temp = Node()
    temp.data = val1
    temp.left = temp.right = None
    return temp
  
v = []
  
# function to insert elements of the 
# tree to map m
def storeInorder(root):
  
    if (root == None):
        return
    storeInorder(root.left)
    v.append(root.data)
    storeInorder(root.right)
  
# function to check if the two BSTs contain
# same set of elements
def checkBSTs(root1, root2):
  
    # Base cases 
    if (root1 == None and root2 == None) :
        return True
    if ((root1 == None and root2 != None) or \
        (root1 != None and root2 == None)):
        return False
          
    # Create two hash sets and store 
    # elements both BSTs in them.
    v1 = []
    v2 = []
    v = v1
    storeInorder(root1)
    v1 = v
    v = v2
    storeInorder(root2)
    v2 = v
      
    # Return True if both hash sets 
    # contain same elements.
    return (v1 == v2)
  
# Driver code
  
# First BST
root1 = Node_(15)
root1.left = Node_(10)
root1.right = Node_(20)
root1.left.left = Node_(5)
root1.left.right = Node_(12)
root1.right.right = Node_(25)
      
# Second BST
root2 = Node_(15)
root2.left = Node_(12)
root2.right = Node_(20)
root2.left.left = Node_(5)
root2.left.left.right = Node_(10)
root2.right.right = Node_(25)
      
# check if two BSTs have same set of elements
if (checkBSTs(root1, root2)):
    print("YES")
else:
    print("NO")
      
# This code is contributed by SHUBHAMSINGH10


C#




// C# program to check if two BSTs 
// contain same set of elements
using System;
using System.Collections.Generic;
  
class GFG
{
  
// BST Node
class Node
{
    public int data;
    public Node left;
    public Node right;
};
  
// Utility function to create new Node
static Node newNode(int val)
{
    Node temp = new Node();
    temp.data = val;
    temp.left = temp.right = null
    return temp;
}
  
// function to insert elements 
// of the tree to map m
static void storeInorder(Node root,
                         List<int> v)
{
    if (root == null)
        return;
    storeInorder(root.left, v);
    v.Add(root.data);
    storeInorder(root.right, v);
}
  
// function to check if the two BSTs 
// contain same set of elements
static bool checkBSTs(Node root1, 
                      Node root2)
    // Base cases 
    if (root1 == null && root2 == null
        return true;
    if ((root1 == null && root2 != null) || 
        (root1 != null && root2 == null))
        return false;
          
    // Create two vectors and store 
    // inorder traversals of both BSTs 
    // in them.
    List<int> v1 = new List<int>();
    List<int> v2 = new List<int>();
    storeInorder(root1, v1);
    storeInorder(root2, v2);
  
    // Return true if both vectors are
    // identical
    return (v1 == v2);
}
  
// Driver Code
public static void Main(String[] args)
    // First BST
    Node root1 = newNode(15);
    root1.left = newNode(10);
    root1.right = newNode(20);
    root1.left.left = newNode(5);
    root1.left.right = newNode(12);
    root1.right.right = newNode(25);
      
    // Second BST
    Node root2 = newNode(15);
    root2.left = newNode(12);
    root2.right = newNode(20);
    root2.left.left = newNode(5);
    root2.left.left.right = newNode(10);
    root2.right.right = newNode(25);
      
    // check if two BSTs have 
    // same set of elements
    if (checkBSTs(root1, root2))
        Console.Write("YES");
    else
        Console.Write("NO");
}     
}
  
// This code is contributed by Rajput-Ji


Javascript




<script>
  
// JavaScript program to check if two BSTs 
// contain same set of elements
  
    // BST Node
class Node {
    constructor() {
        this.data = 0;
        this.prev = null;
        this.next = null;
    }
}
  
    // Utility function to create new Node
    function newNode(val)
    {
        var temp = new Node();
        temp.data = val;
        temp.left = temp.right = null;
        return temp;
    }
  
    // function to insert elements
    // of the tree to map m
    function storeInorder(root, v) {
        if (root == null)
            return;
        storeInorder(root.left, v);
        v.push(root.data);
        storeInorder(root.right, v);
    }
  
    // function to check if the two BSTs
    // contain same set of elements
    function checkBSTs(root1,  root2) 
    {
        // Base cases
        if (root1 == null && root2 == null)
            return true;
        if ((root1 == null && root2 != null) || 
            (root1 != null && root2 == null))
            return false;
  
        // Create two vectors and store
        // inorder traversals of both BSTs
        // in them.
        var v1 = [];
        var v2 = [];
        storeInorder(root1, v1);
        storeInorder(root2, v2);
  
        // Return true if both vectors are
        // identical
        return (v1 == v2);
    }
  
    // Driver Code
      
        // First BST
        var root1 = newNode(15);
        root1.left = newNode(10);
        root1.right = newNode(20);
        root1.left.left = newNode(5);
        root1.left.right = newNode(12);
        root1.right.right = newNode(25);
  
        // Second BST
        var root2 = newNode(15);
        root2.left = newNode(12);
        root2.right = newNode(20);
        root2.left.left = newNode(5);
        root2.left.left.right = newNode(10);
        root2.right.right = newNode(25);
  
        // check if two BSTs have same set of elements
        if (checkBSTs(root1, root2))
            document.write("YES");
        else
            document.write("NO");
  
// This code is contributed by Rajput-Ji
  
</script>


Output

YES

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

Method 3: We know about an interesting property of BST that inorder traversal of a BST generates a sorted array. So we can do inorder traversals of both the BSTs and generate two arrays and finally, we can compare these two arrays. If both of the arrays are the same then the BSTs have the same set of elements otherwise not.

Implementation:

C++




// CPP program to check if two BSTs contains
// same set of elements
#include<bits/stdc++.h>
using namespace std;
  
// BST Node
struct Node
{
    int data;
    struct Node* left;
    struct Node* right;
};
  
// Utility function to create new Node
Node* newNode(int val)
{
    Node* temp = new Node;
    temp->data = val;
    temp->left = temp->right = NULL; 
    return temp;
}
  
// function to insert elements of the 
// tree to map m
void storeInorder(Node* root, vector<int> &v)
{
    if (!root)
        return;
    storeInorder(root->left, v);
    v.push_back(root->data);
    storeInorder(root->right, v);
}
  
// function to check if the two BSTs contain
// same set  of elements
bool checkBSTs(Node* root1, Node* root2)
    // Base cases 
    if (!root1 && !root2) 
        return true;
    if ((root1 && !root2) || (!root1 && root2))
        return false;
          
    // Create two vectors and store 
    // inorder traversals of both BSTs 
    // in them.
    vector<int> v1, v2;
    storeInorder(root1, v1);
    storeInorder(root2, v2);
  
    // Return true if both vectors are
    // identical
    return (v1 == v2);
}
  
// Driver program to check above functions
int main()
    // First BST
    Node* root1 = newNode(15);
    root1->left = newNode(10);
    root1->right = newNode(20);
    root1->left->left = newNode(5);
    root1->left->right = newNode(12);
    root1->right->right = newNode(25);
      
    // Second BST
    Node* root2 = newNode(15);
    root2->left = newNode(12);
    root2->right = newNode(20);
    root2->left->left = newNode(5);
    root2->left->left->right = newNode(10);
    root2->right->right = newNode(25);
      
    // check if two BSTs have same set of elements
    if (checkBSTs(root1, root2))
        cout << "YES";
    else
        cout << "NO";
    return 0;        
}


Java




// Java program to check if two BSTs
// contain same set of elements
import java.util.*;
  
class GFG {
  
    // BST Node
    static class Node {
        int data;
        Node left;
        Node right;
    };
  
    // Utility function to create new Node
    static Node newNode(int val)
    {
        Node temp = new Node();
        temp.data = val;
        temp.left = temp.right = null;
        return temp;
    }
  
    // function to insert elements
    // of the tree to map m
    static void storeInorder(Node root, Vector<Integer> v)
    {
        if (root == null)
            return;
        storeInorder(root.left, v);
        v.add(root.data);
        storeInorder(root.right, v);
    }
  
    // function to check if the two BSTs
    // contain same set of elements
    static boolean checkBSTs(Node root1, Node root2)
    {
        // Base cases
        if (root1 == null && root2 == null)
            return true;
        if ((root1 == null && root2 != null)
            || (root1 != null && root2 == null))
            return false;
  
        // Create two vectors and store
        // inorder traversals of both BSTs
        // in them.
        Vector<Integer> v1 = new Vector<Integer>();
        Vector<Integer> v2 = new Vector<Integer>();
        storeInorder(root1, v1);
        storeInorder(root2, v2);
  
        // Return true if both vectors are
        // identical
        return (v1.hashCode() == v2.hashCode());
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // First BST
        Node root1 = newNode(15);
        root1.left = newNode(10);
        root1.right = newNode(20);
        root1.left.left = newNode(5);
        root1.left.right = newNode(12);
        root1.right.right = newNode(25);
  
        // Second BST
        Node root2 = newNode(15);
        root2.left = newNode(12);
        root2.right = newNode(20);
        root2.left.left = newNode(5);
        root2.left.left.right = newNode(10);
        root2.right.right = newNode(25);
  
        // check if two BSTs have same set of elements
        if (checkBSTs(root1, root2))
            System.out.print("YES");
        else
            System.out.print("NO");
    }
}
  
// This code is contributed by Rajput-Ji
// Code corrected by Prithi_Raj


Python3




# Python3 program to check if two BSTs contains
# same set of elements
  
# BST Node
class Node:
    def __init__(self):
        self.data = 0
        self.left = None
        self.right = None
  
# Utility function to create Node
def Node_(val1):
  
    temp = Node()
    temp.data = val1
    temp.left = temp.right = None
    return temp
  
v = []
  
# function to insert elements of the 
# tree to map m
def storeInorder(root):
  
    if (root == None):
        return
    storeInorder(root.left)
    v.append(root.data)
    storeInorder(root.right)
  
# function to check if the two BSTs contain
# same set of elements
def checkBSTs(root1, root2):
  
    # Base cases 
    if (root1 == None and root2 == None) :
        return True
    if ((root1 == None and root2 != None) or \
        (root1 != None and root2 == None)):
        return False
          
    # Create two hash sets and store 
    # elements both BSTs in them.
    v1 = []
    v2 = []
    v = v1
    storeInorder(root1)
    v1 = v
    v = v2
    storeInorder(root2)
    v2 = v
      
    # Return True if both hash sets 
    # contain same elements.
    return (v1 == v2)
  
# Driver code
  
# First BST
root1 = Node_(15)
root1.left = Node_(10)
root1.right = Node_(20)
root1.left.left = Node_(5)
root1.left.right = Node_(12)
root1.right.right = Node_(25)
      
# Second BST
root2 = Node_(15)
root2.left = Node_(12)
root2.right = Node_(20)
root2.left.left = Node_(5)
root2.left.left.right = Node_(10)
root2.right.right = Node_(25)
      
# check if two BSTs have same set of elements
if (checkBSTs(root1, root2)):
    print("YES")
else:
    print("NO")
      
# This code is contributed by SHUBHAMSINGH10


C#




// C# program to check if two BSTs 
// contain same set of elements
using System;
using System.Collections.Generic;
  
class GFG
{
  
// BST Node
class Node
{
    public int data;
    public Node left;
    public Node right;
};
  
// Utility function to create new Node
static Node newNode(int val)
{
    Node temp = new Node();
    temp.data = val;
    temp.left = temp.right = null
    return temp;
}
  
// function to insert elements 
// of the tree to map m
static void storeInorder(Node root,
                         List<int> v)
{
    if (root == null)
        return;
    storeInorder(root.left, v);
    v.Add(root.data);
    storeInorder(root.right, v);
}
  
// function to check if the two BSTs 
// contain same set of elements
static bool checkBSTs(Node root1, 
                      Node root2)
    // Base cases 
    if (root1 == null && root2 == null
        return true;
    if ((root1 == null && root2 != null) || 
        (root1 != null && root2 == null))
        return false;
          
    // Create two vectors and store 
    // inorder traversals of both BSTs 
    // in them.
    List<int> v1 = new List<int>();
    List<int> v2 = new List<int>();
    storeInorder(root1, v1);
    storeInorder(root2, v2);
  
    // Return true if both vectors are
    // identical
    return (v1 == v2);
}
  
// Driver Code
public static void Main(String[] args)
    // First BST
    Node root1 = newNode(15);
    root1.left = newNode(10);
    root1.right = newNode(20);
    root1.left.left = newNode(5);
    root1.left.right = newNode(12);
    root1.right.right = newNode(25);
      
    // Second BST
    Node root2 = newNode(15);
    root2.left = newNode(12);
    root2.right = newNode(20);
    root2.left.left = newNode(5);
    root2.left.left.right = newNode(10);
    root2.right.right = newNode(25);
      
    // check if two BSTs have 
    // same set of elements
    if (checkBSTs(root1, root2))
        Console.Write("YES");
    else
        Console.Write("NO");
}     
}
  
// This code is contributed by Rajput-Ji


Javascript




<script>
  
// JavaScript program to check if two BSTs 
// contain same set of elements
  
    // BST Node
class Node {
    constructor() {
        this.data = 0;
        this.prev = null;
        this.next = null;
    }
}
  
    // Utility function to create new Node
    function newNode(val)
    {
        var temp = new Node();
        temp.data = val;
        temp.left = temp.right = null;
        return temp;
    }
  
    // function to insert elements
    // of the tree to map m
    function storeInorder(root, v) {
        if (root == null)
            return;
        storeInorder(root.left, v);
        v.push(root.data);
        storeInorder(root.right, v);
    }
  
    // function to check if the two BSTs
    // contain same set of elements
    function checkBSTs(root1,  root2) 
    {
        // Base cases
        if (root1 == null && root2 == null)
            return true;
        if ((root1 == null && root2 != null) || 
            (root1 != null && root2 == null))
            return false;
  
        // Create two vectors and store
        // inorder traversals of both BSTs
        // in them.
        var v1 = [];
        var v2 = [];
        storeInorder(root1, v1);
        storeInorder(root2, v2);
  
        // Return true if both vectors are
        // identical
        return (v1 == v2);
    }
  
    // Driver Code
      
        // First BST
        var root1 = newNode(15);
        root1.left = newNode(10);
        root1.right = newNode(20);
        root1.left.left = newNode(5);
        root1.left.right = newNode(12);
        root1.right.right = newNode(25);
  
        // Second BST
        var root2 = newNode(15);
        root2.left = newNode(12);
        root2.right = newNode(20);
        root2.left.left = newNode(5);
        root2.left.left.right = newNode(10);
        root2.right.right = newNode(25);
  
        // check if two BSTs have same set of elements
        if (checkBSTs(root1, root2))
            document.write("YES");
        else
            document.write("NO");
  
// This code is contributed by Rajput-Ji
  
</script>


Output

YES

Time Complexity: O( n ). 
Auxiliary Space: O( n ).



Last Updated : 08 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads