DSA Tracker

Problem

Medium

Count Good Nodes in Binary Tree

A medium Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Binary Trees
Sheets
1
Core for
4 roles

The problem

Call a node good when no node on the path from the root down to it holds a larger value (the root is always good). Count the good nodes in the tree.

Example 1

Input
root=[4,2,6,1,3,5,7]
Output
3
Why
Good: 4 (root), 6 (>=4), 7 (>=6). Nodes 2,1,3,5 are below path max.

Example 2

Input
root=[5,3,7,2,4,null,8]
Output
3
Why
Good: 5 (root), 7 (>=5), 8 (>=7). Nodes 3,2,4 are below path max.

Example 3

Input
[1]
Output
1
Why
The root is always a good node.

Constraints

  • The number of nodes in the tree is in the range [1, 105].
  • -104 <= Node.val <= 104

How to think about it

Updated 2026-09-29

A node has no memory of the whole ancestry; it only cares about the single largest value encountered along the path from the root. Carry that running maximum downward. If the current value meets or exceeds that ceiling, count it and raise the ceiling for its descendants.

Approaches, worst first

  1. BFS queue with path maximums

    time O(n) · space O(w)

    Queue tuples of `(node, maxSoFar)`. Dequeue, increment total if `node.val >= maxSoFar`, and enqueue children paired with `max(maxSoFar, node.val)`. Avoids recursion limits on deep trees.

  2. DFS top-down accumulatorWrite this one

    time O(n) · space O(h)

    Recurse with the current node and `maxSoFar`. If node value is at least `maxSoFar`, contribute 1 and update `maxSoFar = max(maxSoFar, node.val)`. Sum results from left and right child calls.

Where people lose marks · 3
  • Initial max set to 0 breaks trees with strictly negative values; seed `maxSoFar` with `root.val` or negative infinity.
  • Using strict inequality `>` instead of `>=`; equal values along a path are good nodes by definition since no ancestor is strictly greater.
  • Mutating a shared global maximum instead of scoping `maxSoFar` to the specific branch path leads to cross-branch contamination.

The theory behind it

Binary Trees — the ground this problem stands on. All Binary Trees problems

What Binary Trees is

A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.

When to reach for it

Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.

How the pattern works

Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.

More Binary Trees problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .