Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Determine if Two Trees are Identical #459

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions Determine if Two Trees are Identical
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;

struct Node
{
int data;
struct Node *left;
struct Node *right;

Node(int x){
data = x;
left = NULL;
right = NULL;
}
};

bool isIdentical(Node* a, Node* b);

// Function to Build Tree
Node *buildTree(string str) {
// Corner Case
if (str.length() == 0 || str[0] == 'N')
return NULL;

// Creating vector of strings from input
// string after spliting by space
vector<string> ip;

istringstream iss(str);
for (string str; iss >> str;)
ip.push_back(str);

// Create the root of the tree
Node *root = new Node(stoi(ip[0]));

// Push the root to the queue
queue<Node *> queue;
queue.push(root);

// Starting from the second element
int i = 1;
while (!queue.empty() && i < ip.size()) {

// Get and remove the front of the queue
Node *currNode = queue.front();
queue.pop();

// Get the current node's value from the string
string currVal = ip[i];

// If the left child is not null
if (currVal != "N") {

// Create the left child for the current node
currNode->left = new Node(stoi(currVal));

// Push it to the queue
queue.push(currNode->left);
}

// For the right child
i++;
if (i >= ip.size())
break;
currVal = ip[i];

// If the right child is not null
if (currVal != "N") {

// Create the right child for the current node
currNode->right = new Node(stoi(currVal));

// Push it to the queue
queue.push(currNode->right);
}
i++;
}

return root;
}

int main() {
int tc;
scanf("%d ", &tc);
while (tc--) {
string str, str1;
getline(cin, str);
Node *rootA = buildTree(str);
getline(cin, str1);
Node *rootB = buildTree(str1);
if (isIdentical(rootA, rootB)) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
return 0;
}// } Driver Code Ends


/* A binary tree node


struct Node
{
int data;
struct Node* left;
struct Node* right;

Node(int x){
data = x;
left = right = NULL;
}
};
*/

/* Should return true if trees with roots as r1 and
r2 are identical */
vector<int> v1,v2;
void f1(Node * r1){
if(!r1)return;
f1(r1->left);
v1.push_back(r1->data);
f1(r1->right);
}
void f2(Node * r2){
if(!r2)return;
f2(r2->left);
v2.push_back(r2->data);
f2(r2->right);
}
bool isIdentical(Node *r1, Node *r2)
{
//Your Code here
f1(r1);f2(r2);
if(v1.size()!=v2.size()){v1.clear();v2.clear();return false;}
else{
for(int i=0;i<v1.size();i++){
if(v1[i]!=v2[i]){v1.clear();v2.clear();return false;}
}
v1.clear();v2.clear();
return true;
}

}