Problem Statement

Pattern:


Solution

public int helper(TreeNode root, int max) {
	int count = 0;
	
	if(root == null) return count;
	if(root.val >= max) count++;
	
	count += helper(root.left, Math.max(max, root.val));
	count += helper(root.right, Math.max(max, root.val));
	return count;
}
 
public int goodNodes(TreeNode root) {
	return helper(root, root.val);
}

Notes