package leetcode;

/**
 * https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/
 */
public class Problem1457 {
    public 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;
        }
    }

    public int pseudoPalindromicPaths(TreeNode root) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1457 prob = new Problem1457();

        TreeNode root = new TreeNode(2);
        root.left = new TreeNode(3);
        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(1);
        root.right = new TreeNode(1);
        root.right.right = new TreeNode(1);
        System.out.println(prob.pseudoPalindromicPaths(root)); // 2

        root = new TreeNode(2);
        root.left = new TreeNode(1);
        root.left.left = new TreeNode(1);
        root.left.right = new TreeNode(3);
        root.left.right.right = new TreeNode(1);
        root.right = new TreeNode(1);
        System.out.println(prob.pseudoPalindromicPaths(root)); // 1

        root = new TreeNode(9);
        System.out.println(prob.pseudoPalindromicPaths(root)); // 1
    }
}

@@ -555,15 +555,95 @@
     
-// TODO
+IntRef answer = new IntRef();%0A        pseudoPalindromicPaths(root, new int%5B9%5D, answer);
 %0A   
@@ -658,9 +658,18 @@
 urn 
-0
+answer.val
 ;%0A  
@@ -678,29 +678,30 @@
 %7D%0A%0A    p
-ublic
+rivate
  static 
 void mai
@@ -696,200 +696,236 @@
 tic 
-void main(String%5B%5D args) %7B%0A        Problem1457 prob = new Problem1457();%0A%0A        TreeNode root = new TreeNode(2);%0A        root.left = new TreeNode(3);%0A        root.left.left = new TreeNode(3)
+class IntRef %7B%0A        private int val;%0A    %7D%0A%0A    private static void pseudoPalindromicPaths(TreeNode root, int%5B%5D counts, IntRef answer) %7B%0A        if (root == null) %7B%0A            return;%0A        %7D%0A        counts%5Broot.val - 1%5D++
 ;%0A  
@@ -922,32 +922,36 @@
 - 1%5D++;%0A        
+if (
 root.left.right 
@@ -939,32 +939,48 @@
 f (root.left
+ == null && root
 .right =
  new TreeNod
@@ -971,378 +971,254 @@
 ht =
+=
  n
-ew TreeNode(1);%0A        root.right = new TreeNode(1);%0A        root.right.right = new TreeNode(1);%0A        System.out.println(prob.pseudoPalindromicPaths(root)); // 2%0A%0A        root = new TreeNode(2);%0A        root.left = new TreeNode(1);%0A        root.left.left = new TreeNode(1);%0A        root.left.right = new TreeNode(3);%0A        root.left.right.right = new TreeNode(1)
+ull) %7B%0A            int odd = 0;%0A            for (int i = 0; i %3C counts.length; i++) %7B%0A                if (counts%5Bi%5D %25 2 != 0) %7B%0A                    odd++;%0A                %7D%0A            %7D%0A            if (odd %3C= 1) %7B%0A                answer.val++
 ;%0A  
@@ -1227,206 +1227,199 @@
     
+ 
-root.right = new TreeNode(1);%0A        System.out.println(prob.pseudoPalindromicPaths(root)); // 1%0A%0A        root = new TreeNode(9);%0A        System.out.println(prob.pseudoPalindromicPaths(root)); // 1
+   %7D%0A        %7D else %7B%0A            pseudoPalindromicPaths(root.left, counts, answer);%0A            pseudoPalindromicPaths(root.right, counts, answer);%0A        %7D%0A        counts%5Broot.val - 1%5D--;
 %0A   

Solve problem 1457