forked from Raju1822/Happy-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidentiacl tree.cpp
More file actions
87 lines (81 loc) · 1.38 KB
/
identiacl tree.cpp
File metadata and controls
87 lines (81 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<bits/stdc++.h>
using namespace std;
struct Bstnode{
int data;
Bstnode *left;
Bstnode *right;
};
Bstnode *GetNewNode(int data)
{
Bstnode *newnode=new Bstnode();
newnode->data=data;
newnode->left=newnode->right=NULL;
return newnode;
}
Bstnode *Insert(Bstnode *root,int data)
{
if(root==NULL)
{
root=GetNewNode(data);
}
else if(data<=root->data)
{
root->left=Insert(root->left,data);
}
else
{
root->right=Insert(root->right,data);
}
return root;
}
int identical(Bstnode *r1,Bstnode *r2)
{
if(r1==NULL&&r2==NULL)
{
return 1;
}
if(r1!=NULL&&r2!=NULL)
{
// in order
// left
// root
// right
if(identical(r1->left,r2->left))
{
if(r1->data==r2->data)
if(identical(r1->right,r2->right))
return 1;
}
}
return 0;
}
int main()
{
Bstnode *root1=NULL,*root2=NULL;
int n,a,i,diameter;
cout<<"Enter no. of nodes of 1 tree = ";
cin>>n;
cout<<"Enter the value of all the nodes = \n";
for(i=0;i<n;i++)
{
cin>>a;
root1=Insert(root1,a);
}
cout<<"Enter no. of nodes of 2 tree = ";
cin>>n;
cout<<"Enter the value of all the nodes = \n";
for(i=0;i<n;i++)
{
cin>>a;
root2=Insert(root2,a);
}
int t=identical(root1,root2);
if(t)
{
cout<<"1 and 2 tree are identcial"<<endl;
}
else
{
cout<<"1 and 2 are not identcial"<<endl;
}
}