Skip to content

Commit a7603b5

Browse files
authored
Create 1457-pseudo-palindromic-paths-in-a-binary-tree.kt
1 parent cc4b0a5 commit a7603b5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
fun pseudoPalindromicPaths (root: TreeNode?): Int {
3+
val count = IntArray (10)
4+
var odd = 0
5+
6+
fun dfs(node: TreeNode?): Int {
7+
node ?: return 0
8+
9+
count[node.`val`]++
10+
val oddChange = if (count[node.`val`] % 2 == 1) 1 else -1
11+
odd += oddChange
12+
13+
var res = 0
14+
if (node.left == null && node.right == null)
15+
res = if (odd <= 1) 1 else 0
16+
else
17+
res = dfs(node.left) + dfs(node.right)
18+
19+
odd -= oddChange
20+
count[node.`val`]--
21+
22+
return res
23+
}
24+
25+
return dfs(root)
26+
}
27+
}

0 commit comments

Comments
 (0)