Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e0b6960

Browse files
feat: add No.1457
1 parent 2b4d8da commit e0b6960

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# 1457. Pseudo-Palindromic Paths in a Binary Tree
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Bit Manipulation, Tree, Depth-First Search, Breadth-First Search, Binary Tree.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be **pseudo-palindromic** if at least one permutation of the node values in the path is a palindrome.
10+
11+
**Return the number of **pseudo-palindromic** paths going from the root node to leaf nodes.**
12+
13+
14+
Example 1:
15+
16+
17+
![](https://assets.leetcode.com/uploads/2020/05/06/palindromic_paths_1.png)
18+
19+
20+
```
21+
Input: root = [2,3,1,3,1,null,1]
22+
Output: 2
23+
Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the red path [2,3,3], the green path [2,1,1], and the path [2,3,1]. Among these paths only red path and green path are pseudo-palindromic paths since the red path [2,3,3] can be rearranged in [3,2,3] (palindrome) and the green path [2,1,1] can be rearranged in [1,2,1] (palindrome).
24+
```
25+
26+
Example 2:
27+
28+
29+
![](https://assets.leetcode.com/uploads/2020/05/07/palindromic_paths_2.png)
30+
31+
32+
```
33+
Input: root = [2,1,1,1,3,null,null,null,null,null,1]
34+
Output: 1
35+
Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the green path [2,1,1], the path [2,1,3,1], and the path [2,1]. Among these paths only the green path is pseudo-palindromic since [2,1,1] can be rearranged in [1,2,1] (palindrome).
36+
```
37+
38+
Example 3:
39+
40+
```
41+
Input: root = [9]
42+
Output: 1
43+
```
44+
45+
46+
**Constraints:**
47+
48+
49+
50+
- The number of nodes in the tree is in the range `[1, 105]`.
51+
52+
- `1 <= Node.val <= 9`
53+
54+
55+
56+
## Solution
57+
58+
```javascript
59+
/**
60+
* Definition for a binary tree node.
61+
* function TreeNode(val, left, right) {
62+
* this.val = (val===undefined ? 0 : val)
63+
* this.left = (left===undefined ? null : left)
64+
* this.right = (right===undefined ? null : right)
65+
* }
66+
*/
67+
/**
68+
* @param {TreeNode} root
69+
* @return {number}
70+
*/
71+
var pseudoPalindromicPaths = function(root) {
72+
return dfs(root, Array(10).fill(0), 0);
73+
};
74+
75+
var dfs = function(node, map, numOfOdd) {
76+
if (!node) return 0;
77+
if (map[node.val] % 2) {
78+
numOfOdd -= 1;
79+
} else {
80+
numOfOdd += 1;
81+
}
82+
if (!node.left && !node.right) {
83+
return numOfOdd <= 1 ? 1 : 0;
84+
}
85+
map[node.val] += 1;
86+
var res = dfs(node.left, map, numOfOdd)
87+
+ dfs(node.right, map, numOfOdd);
88+
map[node.val] -= 1;
89+
return res;
90+
};
91+
```
92+
93+
**Explain:**
94+
95+
nope.
96+
97+
**Complexity:**
98+
99+
* Time complexity : O(n).
100+
* Space complexity : O(1).

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /