Skip to content

Commit 89df16a

Browse files
committedMay 29, 2020
Create 1457.二叉树中的伪回文路径.js
1 parent 6057de1 commit 89df16a

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
 
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// LeetCode 有 BUG, 最后一个测试用例直接 return 0 都会超时
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* function TreeNode(val, left, right) {
6+
* this.val = (val===undefined ? 0 : val)
7+
* this.left = (left===undefined ? null : left)
8+
* this.right = (right===undefined ? null : right)
9+
* }
10+
*/
11+
/**
12+
* @param {TreeNode} root
13+
* @return {number}
14+
*/
15+
var pseudoPalindromicPaths = function(root) {
16+
const count = new Array(10).fill(0);
17+
let ji = 0;
18+
let result = 0;
19+
function dfs(node) {
20+
if (node === null) {
21+
return;
22+
}
23+
count[node.val]++;
24+
const x = count[node.val] % 2 === 1 ? 1 : -1;
25+
ji += x;
26+
if (!node.left && !node.right) {
27+
if (ji <= 1) {
28+
result++;
29+
}
30+
} else {
31+
node.left && dfs(node.left);
32+
node.right && dfs(node.right);
33+
}
34+
count[node.val]--;
35+
ji -= x;
36+
}
37+
dfs(root);
38+
return result;
39+
};

0 commit comments

Comments
 (0)
Please sign in to comment.