From c270fe56993a1ba0d52ca3ae34775f3ce56be6cb Mon Sep 17 00:00:00 2001 From: Udhay <72250606+Udhay-Brahmi@users.noreply.github.com> Date: Fri, 25 Dec 2020 08:01:10 +0530 Subject: [PATCH] Create Determine if Two Trees are Identical --- Determine if Two Trees are Identical | 146 +++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 Determine if Two Trees are Identical diff --git a/Determine if Two Trees are Identical b/Determine if Two Trees are Identical new file mode 100644 index 0000000..660cec9 --- /dev/null +++ b/Determine if Two Trees are Identical @@ -0,0 +1,146 @@ +// { Driver Code Starts +#include +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 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 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 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