forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpseudo-palindromic-paths-in-a-binary-tree.cpp
52 lines (48 loc) · 1.37 KB
/
pseudo-palindromic-paths-in-a-binary-tree.cpp
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
// Time: O(n)
// Space: O(h)
/**
* 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) {
int result = 0;
vector<pair<TreeNode *, int>> stk = {{root, 0}};
while (!stk.empty()) {
auto [node, count] = stk.back(); stk.pop_back();
if (!node) {
continue;
}
count ^= 1 << node->val;
result += int(!node->left && !node->right && (count & (count - 1)) == 0);
stk.emplace_back(node->right, count);
stk.emplace_back(node->left, count);
}
return result;
}
};
// Time: O(n)
// Space: O(h)
class Solution2 {
public:
int pseudoPalindromicPaths (TreeNode* root) {
return dfs(root, 0);
}
private:
int dfs(TreeNode *node, int count) {
if (!node) {
return 0;
}
count ^= 1 << node->val;
return int(!node->left && !node->right && (count & (count - 1)) == 0) +
dfs(node->left, count) + dfs(node->right, count);
}
};