File tree 1 file changed +39
-0
lines changed
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments