Skip to content

Commit 42a8fa6

Browse files
committed
feat: add solutions to lc problem: No.1457
No.1457.Pseudo-Palindromic Paths in a Binary Tree
1 parent 48bb051 commit 42a8fa6

File tree

6 files changed

+485
-2
lines changed

6 files changed

+485
-2
lines changed

solution/1400-1499/1457.Pseudo-Palindromic Paths in a Binary Tree/README.md

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,187 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
先序遍历,统计每条路径上数字出现的次数,要满足伪回文路径,当且仅当路径上最多有一个数字的出现次数为奇数。
55+
5456
<!-- tabs:start -->
5557

5658
### **Python3**
5759

5860
<!-- 这里可写当前语言的特殊实现逻辑 -->
5961

6062
```python
61-
63+
# Definition for a binary tree node.
64+
# class TreeNode:
65+
# def __init__(self, val=0, left=None, right=None):
66+
# self.val = val
67+
# self.left = left
68+
# self.right = right
69+
class Solution:
70+
def pseudoPalindromicPaths(self, root: TreeNode) -> int:
71+
def dfs(root):
72+
if root is None:
73+
return
74+
nonlocal ans, counter
75+
counter[root.val] += 1
76+
if root.left is None and root.right is None:
77+
if sum(1 for i in range(1, 10) if counter[i] % 2 == 1) < 2:
78+
ans += 1
79+
else:
80+
dfs(root.left)
81+
dfs(root.right)
82+
counter[root.val] -= 1
83+
84+
ans = 0
85+
counter = [0] * 10
86+
dfs(root)
87+
return ans
6288
```
6389

6490
### **Java**
6591

6692
<!-- 这里可写当前语言的特殊实现逻辑 -->
6793

6894
```java
95+
/**
96+
* Definition for a binary tree node.
97+
* public class TreeNode {
98+
* int val;
99+
* TreeNode left;
100+
* TreeNode right;
101+
* TreeNode() {}
102+
* TreeNode(int val) { this.val = val; }
103+
* TreeNode(int val, TreeNode left, TreeNode right) {
104+
* this.val = val;
105+
* this.left = left;
106+
* this.right = right;
107+
* }
108+
* }
109+
*/
110+
class Solution {
111+
private int ans;
112+
private int[] counter;
113+
114+
public int pseudoPalindromicPaths (TreeNode root) {
115+
ans = 0;
116+
counter = new int[10];
117+
dfs(root);
118+
return ans;
119+
}
120+
121+
private void dfs(TreeNode root) {
122+
if (root == null) {
123+
return;
124+
}
125+
++counter[root.val];
126+
if (root.left == null && root.right == null) {
127+
if (check(counter)) {
128+
++ans;
129+
}
130+
} else {
131+
dfs(root.left);
132+
dfs(root.right);
133+
}
134+
--counter[root.val];
135+
}
136+
137+
private boolean check(int[] counter) {
138+
int n = 0;
139+
for (int i = 1; i < 10; ++i) {
140+
if (counter[i] % 2 == 1) {
141+
++n;
142+
}
143+
}
144+
return n < 2;
145+
}
146+
}
147+
```
148+
149+
### **C++**
150+
151+
```cpp
152+
/**
153+
* Definition for a binary tree node.
154+
* struct TreeNode {
155+
* int val;
156+
* TreeNode *left;
157+
* TreeNode *right;
158+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
159+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
160+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
161+
* };
162+
*/
163+
class Solution {
164+
public:
165+
int ans;
166+
vector<int> counter;
167+
168+
int pseudoPalindromicPaths (TreeNode* root) {
169+
ans = 0;
170+
counter.resize(10);
171+
dfs(root);
172+
return ans;
173+
}
174+
175+
void dfs(TreeNode* root) {
176+
if (!root) return;
177+
++counter[root->val];
178+
if (!root->left && !root->right)
179+
{
180+
int n = 0;
181+
for (int i = 1; i < 10; ++i)
182+
if (counter[i] % 2 == 1)
183+
++n;
184+
if (n < 2) ++ans;
185+
}
186+
else
187+
{
188+
dfs(root->left);
189+
dfs(root->right);
190+
}
191+
--counter[root->val];
192+
}
193+
};
194+
```
69195
196+
### **Go**
197+
198+
```go
199+
/**
200+
* Definition for a binary tree node.
201+
* type TreeNode struct {
202+
* Val int
203+
* Left *TreeNode
204+
* Right *TreeNode
205+
* }
206+
*/
207+
func pseudoPalindromicPaths(root *TreeNode) int {
208+
ans := 0
209+
counter := make([]int, 10)
210+
var dfs func(root *TreeNode)
211+
dfs = func(root *TreeNode) {
212+
if root == nil {
213+
return
214+
}
215+
counter[root.Val]++
216+
if root.Left == nil && root.Right == nil {
217+
n := 0
218+
for i := 1; i < 10; i++ {
219+
if counter[i]%2 == 1 {
220+
n++
221+
}
222+
}
223+
if n < 2 {
224+
ans++
225+
}
226+
} else {
227+
dfs(root.Left)
228+
dfs(root.Right)
229+
}
230+
counter[root.Val]--
231+
}
232+
dfs(root)
233+
return ans
234+
}
70235
```
71236

72237
### **...**

solution/1400-1499/1457.Pseudo-Palindromic Paths in a Binary Tree/README_EN.md

Lines changed: 164 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,176 @@
5151
### **Python3**
5252

5353
```python
54-
54+
# Definition for a binary tree node.
55+
# class TreeNode:
56+
# def __init__(self, val=0, left=None, right=None):
57+
# self.val = val
58+
# self.left = left
59+
# self.right = right
60+
class Solution:
61+
def pseudoPalindromicPaths(self, root: TreeNode) -> int:
62+
def dfs(root):
63+
if root is None:
64+
return
65+
nonlocal ans, counter
66+
counter[root.val] += 1
67+
if root.left is None and root.right is None:
68+
if sum(1 for i in range(1, 10) if counter[i] % 2 == 1) < 2:
69+
ans += 1
70+
else:
71+
dfs(root.left)
72+
dfs(root.right)
73+
counter[root.val] -= 1
74+
75+
ans = 0
76+
counter = [0] * 10
77+
dfs(root)
78+
return ans
5579
```
5680

5781
### **Java**
5882

5983
```java
84+
/**
85+
* Definition for a binary tree node.
86+
* public class TreeNode {
87+
* int val;
88+
* TreeNode left;
89+
* TreeNode right;
90+
* TreeNode() {}
91+
* TreeNode(int val) { this.val = val; }
92+
* TreeNode(int val, TreeNode left, TreeNode right) {
93+
* this.val = val;
94+
* this.left = left;
95+
* this.right = right;
96+
* }
97+
* }
98+
*/
99+
class Solution {
100+
private int ans;
101+
private int[] counter;
102+
103+
public int pseudoPalindromicPaths (TreeNode root) {
104+
ans = 0;
105+
counter = new int[10];
106+
dfs(root);
107+
return ans;
108+
}
109+
110+
private void dfs(TreeNode root) {
111+
if (root == null) {
112+
return;
113+
}
114+
++counter[root.val];
115+
if (root.left == null && root.right == null) {
116+
if (check(counter)) {
117+
++ans;
118+
}
119+
} else {
120+
dfs(root.left);
121+
dfs(root.right);
122+
}
123+
--counter[root.val];
124+
}
125+
126+
private boolean check(int[] counter) {
127+
int n = 0;
128+
for (int i = 1; i < 10; ++i) {
129+
if (counter[i] % 2 == 1) {
130+
++n;
131+
}
132+
}
133+
return n < 2;
134+
}
135+
}
136+
```
137+
138+
### **C++**
139+
140+
```cpp
141+
/**
142+
* Definition for a binary tree node.
143+
* struct TreeNode {
144+
* int val;
145+
* TreeNode *left;
146+
* TreeNode *right;
147+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
148+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
149+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
150+
* };
151+
*/
152+
class Solution {
153+
public:
154+
int ans;
155+
vector<int> counter;
156+
157+
int pseudoPalindromicPaths (TreeNode* root) {
158+
ans = 0;
159+
counter.resize(10);
160+
dfs(root);
161+
return ans;
162+
}
163+
164+
void dfs(TreeNode* root) {
165+
if (!root) return;
166+
++counter[root->val];
167+
if (!root->left && !root->right)
168+
{
169+
int n = 0;
170+
for (int i = 1; i < 10; ++i)
171+
if (counter[i] % 2 == 1)
172+
++n;
173+
if (n < 2) ++ans;
174+
}
175+
else
176+
{
177+
dfs(root->left);
178+
dfs(root->right);
179+
}
180+
--counter[root->val];
181+
}
182+
};
183+
```
60184
185+
### **Go**
186+
187+
```go
188+
/**
189+
* Definition for a binary tree node.
190+
* type TreeNode struct {
191+
* Val int
192+
* Left *TreeNode
193+
* Right *TreeNode
194+
* }
195+
*/
196+
func pseudoPalindromicPaths(root *TreeNode) int {
197+
ans := 0
198+
counter := make([]int, 10)
199+
var dfs func(root *TreeNode)
200+
dfs = func(root *TreeNode) {
201+
if root == nil {
202+
return
203+
}
204+
counter[root.Val]++
205+
if root.Left == nil && root.Right == nil {
206+
n := 0
207+
for i := 1; i < 10; i++ {
208+
if counter[i]%2 == 1 {
209+
n++
210+
}
211+
}
212+
if n < 2 {
213+
ans++
214+
}
215+
} else {
216+
dfs(root.Left)
217+
dfs(root.Right)
218+
}
219+
counter[root.Val]--
220+
}
221+
dfs(root)
222+
return ans
223+
}
61224
```
62225

63226
### **...**

0 commit comments

Comments
 (0)