-
Notifications
You must be signed in to change notification settings - Fork 3
/
872.cpp
36 lines (36 loc) · 1.06 KB
/
872.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void collectLeaf(TreeNode* root, vector<int>& nodes) {
if (!root) return;
if (root->left || root->right) {
collectLeaf(root->left, nodes);
collectLeaf(root->right, nodes);
}
else {
nodes.push_back(root->val);
}
}
bool leafSimilar(TreeNode* root1, TreeNode* root2) {
vector<int> nodes1;
vector<int> nodes2;
collectLeaf(root1, nodes1);
collectLeaf(root2, nodes2);
if (nodes1.size() != nodes2.size()) return false;
int n = nodes1.size();
for (int i = 0; i < n; ++i) {
if (nodes1[i] != nodes2[i]) return false;
}
return true;
}
};