1457 pass. a very simple search problem
This commit is contained in:
		
							
								
								
									
										68
									
								
								1457-231126-pass/main.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								1457-231126-pass/main.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,68 @@ | |||||||
|  | #include<bits/stdc++.h> | ||||||
|  | using namespace std; | ||||||
|  |  | ||||||
|  | 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) { | ||||||
|  |      const int length_limit = 1e5; | ||||||
|  |         int state[10]={}; | ||||||
|  |         return find(root, state); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     bool judge_is_permutation(int odd,int even){ | ||||||
|  |         if(odd<=1) | ||||||
|  |             return true; | ||||||
|  |         else | ||||||
|  |             return false; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     int find(TreeNode* Node, int* state){ | ||||||
|  |         state[Node->val] += 1; | ||||||
|  |         if(Node->left == NULL && Node->right == NULL){ | ||||||
|  |             int odd = 0; | ||||||
|  |             int even = 0; | ||||||
|  |             for(int i = 1 ; i <= 9 ;i++){ | ||||||
|  |                 if(state[i] > 0){ | ||||||
|  |                     if(state[i]%2) | ||||||
|  |                         odd++; | ||||||
|  |                     else | ||||||
|  |                         even++; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             // for(int i = 1 ;i <=9;i++){ | ||||||
|  |             //     cout<<state[i]<<' '; | ||||||
|  |             // } | ||||||
|  |             cout<<endl; | ||||||
|  |             if(judge_is_permutation(odd,even)){ | ||||||
|  |                 state[Node->val]-=1; | ||||||
|  |                 return 1; | ||||||
|  |             } | ||||||
|  |             else{ | ||||||
|  |                 state[Node->val]-=1; | ||||||
|  |                 return 0; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         int rlt = 0; | ||||||
|  |         if(Node->left!= NULL){ | ||||||
|  |             rlt += find(Node->left,state); | ||||||
|  |         } | ||||||
|  |         if(Node->right!= NULL){ | ||||||
|  |             rlt += find(Node->right,state); | ||||||
|  |         } | ||||||
|  |         state[Node->val]-=1; | ||||||
|  |         return rlt; | ||||||
|  |     } | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | int main(){ | ||||||
|  |     return 0; | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user