-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathduplicate_trees.cpp
More file actions
154 lines (131 loc) · 3.2 KB
/
duplicate_trees.cpp
File metadata and controls
154 lines (131 loc) · 3.2 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// Created by Mayank Parasar on 2020-05-01.
//
/*
* Given a binary tree, find all duplicate subtrees (subtrees with the same value and same structure)
* and return them as a list of list [subtree1, subtree2, ...] where subtree1 is a duplicate of subtree2 etc.
# Looks like
# 1
# / \
# 2 2
# / /
# 3 3
print(dup_trees(n1))
# [[(3), (3)], [(2, (3)), (2, (3))]]
# There are two duplicate trees
#
# 2
# /
# 3
# and just the leaf
#
# 3
* */
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> matrix;
struct node{
int val;
node* left;
node* right;
node(int x): val(x), left(nullptr), right(nullptr)
{ }
};
vector<int> in_order_traversal(node* node_, vector<int>& vec) {
if(node_ == nullptr) {
return vec;
}
else {
in_order_traversal(node_->left, vec);
vec.push_back(node_->val);
in_order_traversal(node_->right, vec);
}
return vec;
}
// populate matrix
void populate_matrix(node* node_) {
if(node_ == nullptr) {
return;
}
else {
vector<int> tmp;
populate_matrix(node_->left);
matrix.push_back(in_order_traversal(node_, tmp));
populate_matrix(node_->right);
}
}
bool compare_vec(vector<int> vec1, vector<int> vec2) {
if(vec1.size() != vec2.size())
return false;
else {
for(int ii =0; ii < vec1.size(); ii++)
if(vec1[ii] != vec2[ii])
return false;
else
continue;
}
return true;
}
vector<pair<int, vector<vector<int>>>> matching_trees;
void populate_matching_trees() {
vector<int> added_idxs;
for(int ii=0; ii< matrix.size(); ii++) {
vector<vector<int>> mat;
// if ii is present in added index then go to next indx
bool skip = false;
for(auto k : added_idxs) {
if (ii == k)
skip = true;
}
if (skip)
continue;
mat.push_back(matrix[ii]);
for(int jj = ii+1; jj < matrix.size(); jj++) {
if(compare_vec(matrix[ii], matrix[jj])) {
mat.push_back(matrix[jj]);
added_idxs.push_back(jj);
}
}
matching_trees.push_back(make_pair(ii, mat));
}
}
int main() {
node* node1 = new node(1);
node* node2 = new node(2);
node* node3 = new node(2);
node* node4 = new node(3);
node* node5 = new node(3);
node1->left = node2;
node1->right = node3;
node2->left = node4;
node3->left = node5;
vector<int> tmp;
vector<int> result = in_order_traversal(node1, tmp);
// for(auto i : result)
// cout << i << " ";
populate_matrix(node1);
for(auto i : matrix) {
for(auto k : i) {
cout << k << " ";
}
cout << endl;
}
populate_matching_trees();
cout << "Mating trees are" << endl;
for(auto i : matching_trees) {
if (i.second.size() == 1) {
continue;
}
else {
cout << i.first << ":" << endl;
}
for(auto j : i.second) {
for(auto k : j) {
cout << k << " ";
}
cout << endl;
}
}
return 0;
}