-
Notifications
You must be signed in to change notification settings - Fork 3
/
1457.cpp
42 lines (42 loc) · 1.15 KB
/
1457.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
37
38
39
40
41
42
/**
* 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:
int res = 0;
bool isPalindromic(vector<int>& hash) {
int even = 0;
int odd = 0;
for (int i = 0; i < 10; ++i) {
if (hash[i] & 1) odd++;
else even++;
}
if (odd > 1) return false;
else return even > 0;
}
void dfs(TreeNode* node, vector<int>& hash) {
if (node == nullptr) return;
hash[node->val]++;
if (node->left == nullptr && node->right == nullptr) {
if (isPalindromic(hash)) res++;
hash[node->val]++;
return;
}
dfs(node->left, hash);
dfs(node->right, hash);
hash[node->val]--;
}
int pseudoPalindromicPaths (TreeNode* root) {
vector<int> hash(10, 0);
dfs(root, hash);
return res;
}
};