1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { int count = 0; public int goodNodes(TreeNode root) { dfs(root, Integer.MIN_VALUE); return count; }
public void dfs(TreeNode root, int preMax){ if (root == null) { return; }
if (root.val >= preMax) { count++; }
dfs(root.right, Math.max(preMax, root.val)); dfs(root.left, Math.max(preMax, root.val)); } }
|