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, true);
    }

    private static int goodNodes(TreeNode root, int parent, boolean good) {
        if (root == null) {
            return 0;
        }
        int val = (root.val > parent || (root.val == parent && good)) ? 1 : 0;
        if (root.val >= parent) {
            return goodNodes(root.left, root.val, true) +
                goodNodes(root.right, root.val, true) + val;
        }
        return goodNodes(root.left, root.val, false) +
            goodNodes(root.right, root.val, false) + 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

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

@@ -568,22 +568,16 @@
 IN_VALUE
-, true
 );%0A    %7D
@@ -634,28 +634,11 @@
 int 
-parent, boolean good
+max
 ) %7B%0A
@@ -715,17 +715,16 @@
 t val = 
-(
 root.val
@@ -729,255 +729,71 @@
 al %3E
- parent %7C%7C (root.val == parent && good)) ? 1 : 0;%0A        if (root.val %3E= parent) %7B%0A            return goodNodes(root.left, root.val, true) +%0A                goodNodes(root.right, root.val, true) + val;%0A        %7D%0A        return goodNodes(root.left
+= max ? 1 : 0;%0A        return goodNodes(root.left, Math.max(max
 , ro
@@ -798,23 +798,17 @@
 root.val
-, false
+)
 ) +%0A    
@@ -841,1447 +841,40 @@
 ht, 
-root.val, false) + val;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1448 prob = new Problem1448();%0A%0A        TreeNode root = new TreeNode(3);%0A        root.left = new TreeNode(1);%0A        root.left.left = new TreeNode(3);%0A        root.right = new TreeNode(4);%0A        root.right.left = new TreeNode(1);%0A        root.right.right = new TreeNode(5);%0A        System.out.println(prob.goodNodes(root)); // 4%0A%0A        root = new TreeNode(3);%0A        root.left = new TreeNode(1);%0A        root.left.left = new TreeNode(1);%0A        root.right = new TreeNode(4);%0A        root.right.left = new TreeNode(1);%0A        root.right.right = new TreeNode(5);%0A        System.out.println(prob.goodNodes(root)); // 3%0A%0A        root = new TreeNode(3);%0A        root.left = new TreeNode(3);%0A        root.left.left = new TreeNode(4);%0A        root.left.right = new TreeNode(2);%0A        System.out.println(prob.goodNodes(root)); // 3%0A%0A        root = new TreeNode(3);%0A        root.left = new TreeNode(3);%0A        root.left.left = new TreeNode(4);%0A        root.left.right = new TreeNode(2);%0A        root.left.right.left = new TreeNode(3);%0A        System.out.println(prob.goodNodes(root)); // 4%0A%0A        root = new TreeNode(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        System.out.println(prob.goodNodes(root)); // 2
+ Math.max(max, root.val)) + val;
 %0A   

Solve problem 1448