Skip to content

Commit 02053e7

Browse files
committed
Add solution #1457
1 parent 7628318 commit 02053e7

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,311 LeetCode solutions in JavaScript
1+
# 1,312 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1113,6 +1113,7 @@
11131113
1453|[Maximum Number of Darts Inside of a Circular Dartboard](./solutions/1453-maximum-number-of-darts-inside-of-a-circular-dartboard.js)|Hard|
11141114
1455|[Check If a Word Occurs As a Prefix of Any Word in a Sentence](./solutions/1455-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.js)|Easy|
11151115
1456|[Maximum Number of Vowels in a Substring of Given Length](./solutions/1456-maximum-number-of-vowels-in-a-substring-of-given-length.js)|Medium|
1116+
1457|[Pseudo-Palindromic Paths in a Binary Tree](./solutions/1457-pseudo-palindromic-paths-in-a-binary-tree.js)|Medium|
11161117
1460|[Make Two Arrays Equal by Reversing Sub-arrays](./solutions/1460-make-two-arrays-equal-by-reversing-sub-arrays.js)|Easy|
11171118
1462|[Course Schedule IV](./solutions/1462-course-schedule-iv.js)|Medium|
11181119
1464|[Maximum Product of Two Elements in an Array](./solutions/1464-maximum-product-of-two-elements-in-an-array.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 1457. Pseudo-Palindromic Paths in a Binary Tree
3+
* https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/
4+
* Difficulty: Medium
5+
*
6+
* Given a binary tree where node values are digits from 1 to 9. A path in the binary tree
7+
* is said to be pseudo-palindromic if at least one permutation of the node values in the
8+
* path is a palindrome.
9+
*
10+
* Return the number of pseudo-palindromic paths going from the root node to leaf nodes.
11+
*/
12+
13+
/**
14+
* Definition for a binary tree node.
15+
* function TreeNode(val, left, right) {
16+
* this.val = (val===undefined ? 0 : val)
17+
* this.left = (left===undefined ? null : left)
18+
* this.right = (right===undefined ? null : right)
19+
* }
20+
*/
21+
/**
22+
* @param {TreeNode} root
23+
* @return {number}
24+
*/
25+
var pseudoPalindromicPaths = function(root) {
26+
const frequency = new Array(10).fill(0);
27+
let count = 0;
28+
29+
traverse(root);
30+
31+
return count;
32+
33+
function traverse(node) {
34+
if (!node) return;
35+
36+
frequency[node.val]++;
37+
38+
if (!node.left && !node.right) {
39+
let oddCount = 0;
40+
for (let i = 1; i <= 9; i++) {
41+
if (frequency[i] % 2 !== 0) oddCount++;
42+
}
43+
if (oddCount <= 1) count++;
44+
}
45+
46+
traverse(node.left);
47+
traverse(node.right);
48+
49+
frequency[node.val]--;
50+
}
51+
};

0 commit comments

Comments
 (0)