# 1457. Pseudo-Palindromic Paths in a Binary Tree

``````/**
* 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 pseudoPalindromicPaths(TreeNode* root) {
vector<int> out;
int res = 0;
searchBT(root, out, res);

return res;
}

void searchBT(TreeNode* root, vector<int>& out, int& res) {
if (root) {
out.push_back(root->val);

if (root->left == nullptr && root->right == nullptr) {
vector<int> cnt(10, 0);
for(auto e : out) cnt[e]++;
int odd = 0;
for(auto e : cnt){
if(e%2 != 0){
odd++;
if(odd > 1) break;
}
}
if(odd <= 1) res++;
}
else {
searchBT(root->left, out, res);
searchBT(root->right, out, res);
}

out.pop_back();
}
}
};
``````

``````/**
* 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 pseudoPalindromicPaths(TreeNode* root) {
searchBT(root);
return res;
}

void searchBT(TreeNode* root) {
if (root) {
cnt[root->val]++;

if (root->left == nullptr && root->right == nullptr) {
int odd = 0;
for(auto e : cnt){
if(e.second % 2 != 0){
odd++;
if(odd > 1) break;
}
}
if(odd <= 1) res++;
}
else {
searchBT(root->left);
searchBT(root->right);
}

cnt[root->val]--;
}
}
private:
int res = 0;
map<int, int> cnt;
};
``````

1457. Pseudo-Palindromic Paths in a Binary Tree

(0)
(0)