package leetcode;

/**
 * https://leetcode.com/problems/count-good-nodes-in-binary-tree/
 */
public class Problem1448 {
    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 goodNodes(TreeNode root) {
        return goodNodes(root, Integer.MIN_VALUE);
    }

    private static int goodNodes(TreeNode root, int parent) {
        if (root == null) {
            return 0;
        }
        int val = root.val >= parent ? 1 : 0;
        return goodNodes(root.left, root.val) + goodNodes(root.right, root.val) + val;
    }

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

        TreeNode root = new TreeNode(3);
//        root.left = new TreeNode(1);
//        root.left.left = new TreeNode(3);
//        root.right = new TreeNode(4);
//        root.right.left = new TreeNode(1);
//        root.right.right = new TreeNode(5);
//        System.out.println(prob.goodNodes(root)); // 4

        root = new TreeNode(3);
        root.left = new TreeNode(1);
        root.left.left = new TreeNode(1);
        root.right = new TreeNode(4);
        root.right.left = new TreeNode(1);
        root.right.right = new TreeNode(5);
        System.out.println(prob.goodNodes(root)); // 3

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

@@ -568,16 +568,22 @@
 IN_VALUE
+, true
 );%0A    %7D
@@ -642,16 +642,30 @@
 t parent
+, boolean good
 ) %7B%0A    
@@ -738,16 +738,17 @@
 t val = 
+(
 root.val
@@ -753,26 +753,96 @@
 al %3E
-= parent ? 1 : 0;%0A
+ parent %7C%7C (root.val == parent && good)) ? 1 : 0;%0A        if (root.val %3E= parent) %7B%0A    
     
@@ -881,19 +881,41 @@
 root.val
-) +
+, true) +%0A               
  goodNod
@@ -937,16 +937,145 @@
 root.val
+, true) + val;%0A        %7D%0A        return goodNodes(root.left, root.val, false) +%0A            goodNodes(root.right, root.val, false
 ) + val;
@@ -1207,34 +1207,32 @@
 ew TreeNode(3);%0A
-//
         root.lef
@@ -1244,34 +1244,32 @@
 ew TreeNode(1);%0A
-//
         root.lef
@@ -1286,34 +1286,32 @@
 ew TreeNode(3);%0A
-//
         root.rig
@@ -1324,34 +1324,32 @@
 ew TreeNode(4);%0A
-//
         root.rig
@@ -1367,34 +1367,32 @@
 ew TreeNode(1);%0A
-//
         root.rig
@@ -1419,18 +1419,16 @@
 ode(5);%0A
-//
         
@@ -1767,18 +1767,16 @@
 ; // 3%0A%0A
-//
         
@@ -1791,34 +1791,32 @@
 ew TreeNode(3);%0A
-//
         root.lef
@@ -1828,34 +1828,32 @@
 ew TreeNode(3);%0A
-//
         root.lef
@@ -1870,34 +1870,32 @@
 ew TreeNode(4);%0A
-//
         root.lef
@@ -1913,34 +1913,32 @@
 ew TreeNode(2);%0A
-//
         System.o
@@ -1976,21 +1976,17 @@
 ); // 3%0A
-//%0A//
+%0A
         
@@ -2001,34 +2001,32 @@
 ew TreeNode(3);%0A
-//
         root.lef
@@ -2038,34 +2038,32 @@
 ew TreeNode(3);%0A
-//
         root.lef
@@ -2088,18 +2088,16 @@
 ode(4);%0A
-//
         
@@ -2131,18 +2131,16 @@
 ode(2);%0A
-//
         
@@ -2179,18 +2179,16 @@
 ode(3);%0A
-//
         
@@ -2238,13 +2238,9 @@
 / 4%0A
-//%0A//
+%0A
     
@@ -2267,18 +2267,185 @@
 ode(1);%0A
-//
+        System.out.println(prob.goodNodes(root)); // 1%0A%0A        root = new TreeNode(9);%0A        root.right = new TreeNode(3);%0A        root.right.left = new TreeNode(6);%0A
         
@@ -2489,17 +2489,17 @@
 t)); // 
-1
+2
 %0A    %7D%0A%7D

Update problem 1448