forked from tecky708/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trees_are_identical_or_not.cpp
60 lines (55 loc) · 1.21 KB
/
trees_are_identical_or_not.cpp
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
#include <iostream>
#include <queue>
using namespace std;
class Node
{
public:
int data;
Node *left;
Node *right;
Node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
Node *createTree1()
{
Node *root = new Node(10);
root->left = new Node(12);
root->left->left = new Node(8);
root->right = new Node(6);
root->right->left = new Node(9);
root->right->right = new Node(10);
return root;
}
Node *createTree2()
{
Node *root = new Node(10);
root->left = new Node(12);
root->left->left = new Node(8);
root->right = new Node(6);
root->right->left = new Node(9);
root->right->right = new Node(10);
return root;
}
bool method1(Node *root1, Node *root2)
{
if (root1 == NULL && root2 == NULL)
{
return true;
}
if (root1 == NULL || root2 == NULL)
{
return false;
}
return (root1->data == root2->data) && method1(root1->left, root2->left) && method1(root1->right, root2->right);
}
int main()
{
Node *root1 = createTree1();
Node *root2 = createTree2();
cout << method1(root1, root2) << endl;
return 0;
}