-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpseudo_palindromic_paths_in_a_binary_tree.dart
272 lines (215 loc) · 6.79 KB
/
pseudo_palindromic_paths_in_a_binary_tree.dart
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
-* Pseudo-Palindromic Paths in a Binary Tree *-
Given a binary tree where node values are digits from 1 to 9.
A path in the binary tree is said to be pseudo-palindromic if at least one permutation of the node
values in the path is a palindrome.
Return the number of pseudo-palindromic paths going from the root node to leaf nodes.
Example 1:
Input: root = [2,3,1,3,1,null,1]
Output: 2
Explanation: The figure above represents the given binary tree.
There are three paths going from the root node to leaf nodes:
the red path [2,3,3], the green path [2,1,1], and the path [2,3,1]. Among these paths only
red path and green path are pseudo-palindromic paths since the red path [2,3,3] can be rearranged in [3,2,3] (palindrome) and
the green path [2,1,1] can be rearranged in [1,2,1] (palindrome).
Example 2:
Input: root = [2,1,1,1,3,null,null,null,null,null,1]
Output: 1
Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the green path [2,1,1], the path [2,1,3,1], and the path [2,1]. Among these paths only the green path is pseudo-palindromic since [2,1,1] can be rearranged in [1,2,1] (palindrome).
Example 3:
Input: root = [9]
Output: 1
Constraints:
The number of nodes in the tree is in the range [1, 105].
1 <= Node.val <= 9
*/
// Definition for a binary tree node.
import 'dart:collection';
class TreeNode {
int val;
TreeNode? left;
TreeNode? right;
TreeNode([this.val = 0, this.left, this.right]);
}
class A {
int answer = 0;
int pseudoPalindromicPaths(TreeNode? root) {
preOrder(root, 0);
return answer;
}
void preOrder(TreeNode? root, int bitMask) {
if (root == null) return;
bitMask ^= (1 << root.val);
if (root.left == null &&
root.right == null &&
((bitMask & (bitMask - 1)) == 0)) {
answer++;
}
preOrder(root.left, bitMask);
preOrder(root.right, bitMask);
}
}
class B {
int pseudoPalindromicPaths(TreeNode? root) {
int count = 0;
void findPaths(TreeNode? root, int path) {
if (root != null) {
path ^= (1 << root.val);
if (root.right == null && root.left == null) {
if ((path & (path - 1)) == 0) count++;
}
findPaths(root.left, path);
findPaths(root.right, path);
path ^= (1 << root.val); //backtracking step
}
}
int path = 0;
findPaths(root, path);
return count;
}
}
class C {
int pseudoPalindromicPaths(TreeNode? root) {
int findPath(TreeNode? root, int mask) {
if (root == null) return 0;
// if root->val has occured odd times set to even times
// else if root->val has occured even times set to odd times
int k = 1 << root.val;
mask = mask ^ k;
if (root.left == null && root.right == null) {
// if leaf node check how many numbers has occured odd times
// if odd occurence are zero or one return true else false
if (mask == 0) return 1;
mask = mask & (mask - 1);
if (mask == 0) return 1;
return 0;
}
return findPath(root.left, mask) + findPath(root.right, mask);
}
int mask = 0;
// zero means every number has occured even times
return findPath(root, mask);
}
}
class D {
int pseudoPalindromicPaths(TreeNode? root) {
int solve(TreeNode? root, HashSet<int> hash) {
if (root == null) return 0;
if (root.left == null && root.right == null) {
// hash[root.val]++;
hash.add(root.val++);
int odd = 0;
// cout<<endl;
for (int i = 0; i < 10; i++) {
// cout<<hash[i]<<" ";
if ((hash.elementAt(i) & 1) == 0) //frequency of digit is odd
{
odd++;
}
if (odd > 1) return 0;
}
return 1;
}
// hash[root.val]++;
hash.add(root.val--);
int left = solve(root.left, hash);
int right = solve(root.right, hash);
return left + right;
}
HashSet<int> hash = [10, 0] as HashSet<int>;
// HashSet<int> h = HashMap.from(h)
return solve(root, hash);
}
}
class E {
int pseudoPalindromicPaths(TreeNode? root) {
int helper(TreeNode? root, List<int> array, int cnt) {
if (root == null) return 0;
cnt += (++array[root.val]) % 2 == 0 ? -1 : 1;
int res = helper(root.left, array, cnt) +
helper(root.right, array, cnt) +
(root.left == null && root.right == null && cnt <= 1 ? 1 : 0);
--array[root.val];
return res;
}
List<int> array = [10];
return helper(root, array, 0);
}
}
class F {
int count = 0;
int pseudoPalindromicPaths(TreeNode? root) {
void dfs(TreeNode? node, HashSet<int> unpaired) {
if (node == null) return;
if (!unpaired.add(node.val)) unpaired.remove(node.val);
if (node.left == null && node.right == null) {
if (unpaired.length <= 1) count++;
}
dfs(node.left, unpaired);
dfs(node.right, unpaired);
}
dfs(root, HashSet());
return count;
}
}
class G {
int pseudoPalindromicPaths(TreeNode? root) {
HashMap<int, int> mp = HashMap();
if (root == null) return 0;
mp[root.val++];
// mp.entries.elementAt(root.val++);
if (root.left == null && root.right == null) {
int count = 0;
// for (int i in mp) if (i.remainder(2) != 0) count++;
mp.forEach((key, value) {
if ((mp[key]! % 2) == 1) count++;
});
mp[root.val--];
if (count <= 1) return 1;
return 0;
}
int x =
pseudoPalindromicPaths(root.left) + pseudoPalindromicPaths(root.right);
mp[root.val--];
return x;
}
}
class H {
int pseudoPalindromicPaths(TreeNode? root) {
int count = 0;
recursiveFunction(TreeNode? node, Map<int, dynamic> map) {
if (node == null) return;
// storing/updating the frequency on map
map[node.val] = (map[node.val] == map[node.val] || map[node.val] == 1);
// If we are reaching at the end of the leaf we have to check the frequencies
if (node.left == null && node.right == null) {
bool? frequency = false;
// for (int key in map) {
// // checking character having odd frequency for only one character.
// if (map[key] % 2 == 1) {
// if (!frequency)
// frequency = true;
// else
// return;
// }
// }
map.forEach((key, value) {
if (map[key] % 2 == 1) {
if (frequency == null) {
frequency = true;
} else {
return;
}
}
});
count++;
}
// passing the hashmap to the child elements
if (node.left == 1) recursiveFunction(node.left, {...map});
if (node.right == 1) recursiveFunction(node.right, {...map});
}
;
recursiveFunction(root, {});
return count;
}
}