-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5b3cb9
commit 7a8bf98
Showing
1 changed file
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
public class Hello { | ||
|
||
static class TreeNode { | ||
|
||
int val; | ||
|
||
TreeNode left; | ||
TreeNode right; | ||
|
||
TreeNode() { | ||
|
||
} | ||
|
||
TreeNode(int val) { | ||
|
||
this.val = val; | ||
|
||
} | ||
|
||
TreeNode(int val, TreeNode left, TreeNode right) { | ||
|
||
this.val = val; | ||
this.left = left; | ||
this.right = right; | ||
|
||
} | ||
|
||
} | ||
|
||
static int countPseudoPalindromicPaths = 0; | ||
|
||
private static boolean isPathPalindromic(int[] pathFreq) { | ||
|
||
int numberOfOddFreq = 0; | ||
|
||
for (int idx = 0; idx < 10; ++idx) { | ||
|
||
if (pathFreq[idx] % 2 != 0) { | ||
|
||
++numberOfOddFreq; | ||
|
||
} | ||
|
||
} | ||
|
||
if (numberOfOddFreq <= 1) { | ||
|
||
return true; | ||
|
||
} | ||
|
||
return false; | ||
|
||
} | ||
|
||
private static void allPaths(TreeNode root, int[] pathFreq) { | ||
|
||
if (root == null) { | ||
|
||
return; | ||
|
||
} | ||
|
||
++pathFreq[root.val - 1]; | ||
|
||
if (root.left == null && root.right == null) { | ||
|
||
if (isPathPalindromic(pathFreq)) { | ||
|
||
++countPseudoPalindromicPaths; | ||
|
||
} | ||
|
||
} else { | ||
|
||
allPaths(root.left, pathFreq); | ||
allPaths(root.right, pathFreq); | ||
|
||
} | ||
|
||
--pathFreq[root.val - 1]; | ||
|
||
} | ||
|
||
public int pseudoPalindromicPaths(TreeNode root) { | ||
|
||
int[] pathFreq = new int[9]; | ||
|
||
allPaths(root, pathFreq); | ||
|
||
int count = countPseudoPalindromicPaths; | ||
|
||
countPseudoPalindromicPaths = 0; | ||
|
||
return count; | ||
|
||
} | ||
|
||
} |