generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.cpp
35 lines (31 loc) · 835 Bytes
/
solution.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
#include <list>
class Solution {
public:
bool leafSimilar(TreeNode* root1, TreeNode* root2) {
std::list<int> leaves1, leaves2;
pushLeaves(leaves1, root1);
pushLeaves(leaves2, root2);
auto it1 = leaves1.begin();
auto it2 = leaves2.begin();
while (it1 != leaves1.end() && it2 != leaves2.end()) {
if (*it1 != *it2) return false;
++it1;
++it2;
}
return it1 == leaves1.end() && it2 == leaves2.end();
}
void pushLeaves(std::list<int>& leaves, TreeNode* node) {
if (node->left != nullptr) {
pushLeaves(leaves, node->left);
if (node->right != nullptr) {
pushLeaves(leaves, node->right);
}
} else {
if (node->right != nullptr) {
pushLeaves(leaves, node->right);
} else {
leaves.push_back(node->val);
}
}
}
};