-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay55.cpp
More file actions
49 lines (49 loc) · 1.34 KB
/
Day55.cpp
File metadata and controls
49 lines (49 loc) · 1.34 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
/**
* 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 check(vector<int>& temp , int& ans){
int cnt = 0;
for(auto& i : temp){
if(i % 2) {
cnt++;
}
}
if(cnt <= 1){
ans++;
}
return;
}
void solve(TreeNode* root, int &ans, vector<int> &temp){
if(!root -> left && !root -> right){
check(temp, ans);
return ;
}
if(root -> left){
temp[root -> left -> val]++;
solve(root -> left , ans , temp);
temp[root -> left -> val]++;
}
if(root -> right){
temp[root -> right -> val]++;
solve(root -> right , ans , temp);
temp[root -> right -> val]--;
}
}
int pseudoPalindromicPaths (TreeNode* root) {
int ans = 0;
vector<int> temp(10, 0);
temp[root -> val]++;
solve(root, ans, temp);
return ans;
}
};