Count Good Nodes in Binary Tree

Medium

Topics
TreeDFSBFS

A node is good if no node on the path from the root to it has a greater value. Given the root, return the number of good nodes.

Example 1

313145
good node
A node is good when nothing on the path from the root to it is larger. The root is always good.
Tree given in level-order (breadth-first); null marks a missing child.
Input:  root = [3,1,4,3,null,1,5]
Output: 4

Example 2

4323
good node
Input:  root = [3,3,null,4,2]
Output: 3

Constraints

  • 1 <= number of nodes <= 10^5
  • -10^4 <= Node.val <= 10^4
Editorial — intuition, approach, complexity and pitfalls

Intuition

A node is good exactly when it is at least as large as the biggest value seen on the way down. Carry that running maximum as you descend.

Approach

  1. DFS from the root carrying maxSoFar, starting at the root value (or -inf).
  2. At each node, if node.val >= maxSoFar it is good; update the max.
  3. Sum the good count over both subtrees.

Dry Run

tree 3 1 4 3 null 1 5
      3
     / \
    1   4
   /   / \
  3   1   5
The maximum along the path is carried DOWN; each node compares itself with what
came before it:
  node 3   max so far 3   GOOD    (the root is always good)
    node 1   max so far 3   not good
      node 3   max so far 3   GOOD    equal to the max, which still counts
    node 4   max so far 3   GOOD
      node 1   max so far 4   not good
      node 5   max so far 4   GOOD
answer 4
The left-subtree 3 is the row that matters. It equals the running maximum rather
than exceeding it, and the problem counts that as good -- so the comparison is >=
and not >. Strict greater-than drops every tie and this tree returns 2 instead
of 4.
Note the max is per-path, not global. The 1 beneath the 4 is compared against 4,
not against the 5 that sits beside it, because a different branch is not "on the
way" to this node. That is why mx is a parameter rather than an attribute.
Seeding the walk with root.val instead of negative infinity makes no difference,
which is worth knowing before hunting for a bug: the root is >= itself so it
counts either way, and mx becomes root.val on the first step regardless.

Complexity

  • Time — O(n): each node visited once.
  • Space — O(h): recursion stack.

Common Pitfalls

  • The comparison is >=, not >. A node equal to the maximum seen so far still counts as good, and using strict greater than drops every tie -- 2 instead of 4 on the first example.
  • Seeding the walk with float('-inf') rather than root.val is not a bug, which is worth knowing before you hunt for one: the root is >= anything, so it counts either way, and mx becomes root.val on the first step regardless.

Full Solution — all languages

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def goodNodes(self, root):
        def dfs(node,mx):
            if not node: return 0
            good=1 if node.val>=mx else 0
            mx=max(mx,node.val)
            return good+dfs(node.left,mx)+dfs(node.right,mx)
        return dfs(root,root.val)
SensAI
Beta
Tap the mic to stop voice mode