2Brute2Best
🔥0

Count Good Nodes in Binary Tree

Medium·LeetCode #1448 Pro OnlyPattern Transfer
LeetCode
01

Problem Statement

You are given the root of a binary tree.

A node X is good if no node on the path from the root down to X has a value greater than X's value. The root is always good. Return how many good nodes the tree has.

Input

The root of a binary tree. It has at least one node.

Output

The number of good nodes.

Example 1
Input: root = [3, 1, 4, 3, null, 1, 5]
Output: 4

The good nodes are the root 3, the 3 under 1 (path 3, 1, 3), 4 (path 3, 4) and 5 (path 3, 4, 5). The 1 under 4 is not good, because 4 is bigger.

Example 2
Input: root = [3, 3, null, 4, 2]
Output: 3

Node 2 has the path 3, 3, 2. The 3 above it is bigger than 2, so 2 is not good. The other three nodes are good.

Example 3
Input: root = [1]
Output: 1

The only node is the root, which is always good.

Constraints
  • • The tree has between 1 and 10^5 nodes.
  • • −10^4 ≤ Node.val ≤ 10^4
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“no node on the path is greater”
→ The rule compares a node with the whole path above it, so the state to carry is the biggest value on that path.
“the path from the root to X”
→ A top-down path, so the state flows from the root to the leaves.
“count the good nodes”
→ Add 1 for each node that passes the test.
03

Approach Complexity Ladder

Brute Force — re-walk each root → node pathO(N · H)O(H)

For every node, read the path from the root again and find its biggest value

Optimal — carry the biggest value downO(N)O(H)

Pass down the biggest value above each node; a node is good when it is at least that

Brute Force Intuition

Keep the path from the root in a stack. For each node, walk the whole stack again to find the biggest value on its path, including the node itself. If the node's value equals or beats that biggest value, it is good.

Recognition rule for this sub-pattern:

Carry information down the path (current sum, max so far, direction). Check it at leaves.
Remember this · Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.