-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisSubtree.cpp
More file actions
111 lines (104 loc) · 2.91 KB
/
Copy pathisSubtree.cpp
File metadata and controls
111 lines (104 loc) · 2.91 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
/**
* https://leetcode.com/problems/subtree-of-another-tree/
* Tree, DFS, String Matching, Binary Tree, Hash Function
* Easy
*/
#include <iostream>
#include <queue>
#include <string>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int n) : val(n), left(nullptr), right(nullptr) {}
TreeNode(int n, TreeNode* l, TreeNode* r) : val(n), left(l), right(r) {}
};
class Solution {
public:
bool isIdentical(TreeNode* root, TreeNode* subRoot) {
if (!root && !subRoot) {
return true;
}
if ((root && !subRoot) || (!root && subRoot)) {
return false;
}
if (root->val != subRoot->val) {
return false;
}
return isIdentical(root->left, subRoot->left) && isIdentical(root->right, subRoot->right);
}
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
if (!root) {
return false;
}
if (isIdentical(root, subRoot)) {
return true;
}
return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
}
};
TreeNode* buildTree(vector<string>& nodes, int& index) {
if (index >= nodes.size() || nodes[index] == "null") {
index++;
return nullptr;
}
int currNode = stoi(nodes[index]);
TreeNode* root = new TreeNode(currNode);
index++;
root->left = buildTree(nodes, index);
root->right = buildTree(nodes, index);
return root;
}
TreeNode* buildTree(vector<string>& head) {
int index = 0;
TreeNode* root = buildTree(head, index);
return root;
}
void printTree(TreeNode* root) {
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
cout << endl << "Binary tree:" << endl;
while (!nodeQueue.empty()) {
TreeNode* currNode = nodeQueue.front();
if (currNode) {
cout << currNode->val << " ";
if (currNode->left) {
nodeQueue.push(currNode->left);
}
if (currNode->right) {
nodeQueue.push(currNode->right);
}
} else {
cout << "NULL" << " ";
}
nodeQueue.pop();
}
cout << endl;
}
int main() {
TreeNode *root, *subRoot;
vector<string> rootNodes;
string rootNode;
cout << "Input binary tree for root:" << endl;
while (cin >> rootNode && rootNode != "end") {
rootNodes.push_back(rootNode);
}
root = buildTree(rootNodes);
printTree(root);
vector<string> subrootNodes;
string subrootNode;
cout << "Input binary tree for sub-root:" << endl;
while (cin >> subrootNode && subrootNode != "end") {
subrootNodes.push_back(subrootNode);
}
subRoot = buildTree(subrootNodes);
Solution solution;
bool isSubtree = solution.isSubtree(root, subRoot);
if (isSubtree) {
cout << "is subtree" << endl;
} else {
cout << "is not subtree" << endl;
}
}