-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path1457-PseudoPalindromicPathsInABinaryTree.cs
57 lines (48 loc) · 1.47 KB
/
1457-PseudoPalindromicPathsInABinaryTree.cs
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
53
54
55
56
57
//-----------------------------------------------------------------------------
// Runtime: 196ms
// Memory Usage: 45.9 MB
// Link: https://leetcode.com/submissions/detail/363095932/
//-----------------------------------------------------------------------------
namespace LeetCode
{
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class _1457_PseudoPalindromicPathsInABinaryTree
{
private int[] counts = new int[10];
private int result = 0;
public int PseudoPalindromicPaths(TreeNode root)
{
PreOrder(root);
return result;
}
private void PreOrder(TreeNode root)
{
if (root == null) return;
counts[root.val]++;
if (root.left == null && root.right == null)
result += IsPalindromicPath() ? 1 : 0;
PreOrder(root.left);
PreOrder(root.right);
counts[root.val]--;
}
private bool IsPalindromicPath()
{
var odd = 0;
foreach (var val in counts)
odd += val % 2 == 1 ? 1 : 0;
return odd <= 1;
}
}
}