Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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 ans=0;
- bool checkPal(map<int,int> m){
- int odd=0;
- int even=0;
- for(auto iter:m){
- if(iter.second%2!=0){
- odd++;
- }else{
- even++;
- }
- }
- return odd<=1;
- }
- void dfs(TreeNode* root,map<int,int> m){
- if(root==nullptr){
- return;
- }
- m[root->val]++;
- if(root->left==nullptr&&root->right==nullptr){
- if(checkPal(m)){
- ans++;
- }
- }
- dfs(root->left,m);
- dfs(root->right,m);
- return;
- }
- int pseudoPalindromicPaths (TreeNode* root) {
- map<int,int> m;
- m.clear();
- dfs(root,m);
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment