-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY3
More file actions
38 lines (24 loc) · 800 Bytes
/
DAY3
File metadata and controls
38 lines (24 loc) · 800 Bytes
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
class Solution {
public:
int pseudoPalindromicPaths(TreeNode* root) {
int count = 0, path = 0;
stack<pair<TreeNode*, int>> stk;
stk.push({root, 0});
while (!stk.empty()) {
auto [node, path] = stk.top();
stk.pop();
if (node != nullptr) {
path = path ^ (1 << node->val);
if (node->left == nullptr && node->right == nullptr) {
if ((path & (path - 1)) == 0) {
++count;
}
} else {
stk.push({node->right, path});
stk.push({node->left, path});
}
}
}
return count;
}
};