We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cc4b0a5 commit a7603b5Copy full SHA for a7603b5
kotlin/1457-pseudo-palindromic-paths-in-a-binary-tree.kt
@@ -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