diff --git a/src/main/java/com/thealgorithms/datastructures/trees/Create Count Good Nodes in Binary Tree b/src/main/java/com/thealgorithms/datastructures/trees/Create Count Good Nodes in Binary Tree new file mode 100644 index 000000000000..52644a4ace5d --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/trees/Create Count Good Nodes in Binary Tree @@ -0,0 +1,17 @@ +class Solution { + + public int goodNodes(TreeNode root) { + return helper(root, -99999); + } + + public int helper(TreeNode root, int max) { + if (root == null) return 0; + + int res = root.val >= max ? 1 : 0; + + res += helper(root.left, Math.max(root.val, max)); + res += helper(root.right, Math.max(root.val, max)); + + return res; + } +}