forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
35 lines (35 loc) · 910 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
/**
* Definition for a binary tree node.
* struct Node {
* char val;
* Node *left;
* Node *right;
* Node() : val(' '), left(nullptr), right(nullptr) {}
* Node(char x) : val(x), left(nullptr), right(nullptr) {}
* Node(char x, Node *left, Node *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool checkEquivalence(Node* root1, Node* root2) {
int cnt[26]{};
function<void(Node*, int)> dfs = [&](Node* root, int v) {
if (!root) {
return;
}
if (root->val != '+') {
cnt[root->val - 'a'] += v;
}
dfs(root->left, v);
dfs(root->right, v);
};
dfs(root1, 1);
dfs(root2, -1);
for (int& x : cnt) {
if (x) {
return false;
}
}
return true;
}
};