Count Good Nodes in Binary Tree LeetCode solution in Java
1class Solution {2int ans = 0;34public int goodNodes(TreeNode root) {5if (root == null) return 0;6dfs(root, root.val);7return ans;8}910void dfs(TreeNode root, int mx) {11if (root == null) return;1213mx = Math.max(mx, root.val);14if (mx <= root.val) ans++;1516dfs(root.left, mx);17dfs(root.right, mx);18}19}