-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
52 lines (48 loc) · 1.04 KB
/
Copy pathSolution.java
File metadata and controls
52 lines (48 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution
{
private int count = 0;
public int pseudoPalindromicPaths(TreeNode root)
{
count = 0;
final int[] counter = new int[11];
_find(root, counter);
return count;
}
private void _find(TreeNode root, int[] counter)
{
if (root == null)
{
return;
}
counter[root.val]++;
if (root.left == null && root.right == null)
{
if (_isPalindrome(counter))
{
count++;
}
}
else
{
_find(root.left, counter);
_find(root.right, counter);
}
counter[root.val]--;
}
private boolean _isPalindrome(int[] counter)
{
boolean foundOdd = false;
for (int i = 1; i <= 10; i++)
{
if (counter[i] % 2 != 0)
{
if (foundOdd)
{
return false;
}
foundOdd = true;
}
}
return true;
}
}