Skip to content

Commit 92059e4

Browse files
add 1457
1 parent d02685c commit 92059e4

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11-
|1456|[ Maximum Number of Vowels in a Substring of Given Length](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1456.java) | |Easy|String, Sliding Window|
11+
|1457|[Pseudo-Palindromic Paths in a Binary Tree](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1457.java) | |Medium|Bit Manipulation, Tree, DFS|
12+
|1456|[Maximum Number of Vowels in a Substring of Given Length](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1456.java) | |Medium|String, Sliding Window|
1213
|1455|[Check If a Word Occurs As a Prefix of Any Word in a Sentence](https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1455.java) | |Easy|String|
1314
|1452|[People Whose List of Favorite Companies Is Not a Subset of Another List](https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1452.java) | |Medium|String, Sort|
1415
|1451|[Rearrange Words in a Sentence](https://leetcode.com/problems/rearrange-words-in-a-sentence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1451.java) | |Medium|String, Sort|
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
public class _1457 {
11+
public static class Solution1 {
12+
public int pseudoPalindromicPaths(TreeNode root) {
13+
List<List<Integer>> allPaths = new ArrayList<>();
14+
List<Integer> path = new ArrayList<>();
15+
dfs(root, path, allPaths);
16+
int result = 0;
17+
for (List<Integer> thisPath : allPaths) {
18+
Map<Integer, Integer> count = findCount(thisPath);
19+
int oddCount = 0;
20+
for (int num : count.keySet()) {
21+
if (count.get(num) % 2 != 0) {
22+
oddCount++;
23+
}
24+
if (oddCount > 1) {
25+
break;
26+
}
27+
}
28+
if (oddCount <= 1) {
29+
result++;
30+
}
31+
}
32+
return result;
33+
}
34+
35+
private void dfs(TreeNode root, List<Integer> path, List<List<Integer>> allPaths) {
36+
if (root.left == null && root.right == null) {
37+
path.add(root.val);
38+
allPaths.add(new ArrayList<>(path));
39+
return;
40+
}
41+
path.add(root.val);
42+
if (root.left != null) {
43+
dfs(root.left, path, allPaths);
44+
path.remove(path.size() - 1);
45+
}
46+
if (root.right != null) {
47+
dfs(root.right, path, allPaths);
48+
path.remove(path.size() - 1);
49+
}
50+
}
51+
52+
private Map<Integer, Integer> findCount(List<Integer> path) {
53+
Map<Integer, Integer> map = new HashMap<>();
54+
path.forEach(i -> map.put(i, map.getOrDefault(i, 0) + 1));
55+
return map;
56+
}
57+
}
58+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._1456;
6+
import com.fishercoder.solutions._1457;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import java.util.Arrays;
11+
12+
import static junit.framework.TestCase.assertEquals;
13+
14+
public class _1457Test {
15+
private static _1457.Solution1 solution1;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _1457.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
TreeNode root = TreeUtils.constructBinaryTree(Arrays.asList(2, 3, 1, 3, 1, null, 1));
25+
assertEquals(2, solution1.pseudoPalindromicPaths(root));
26+
}
27+
28+
}

0 commit comments

Comments
 (0)