</>

1448. Count Good Nodes in Binary Tree

Medium NeetCode 150
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Trees
Time
—
Space
—

Standard Solution (C++)

SummaryCount the nodes in a binary tree where no ancestor along the root-to-node path has a strictly greater value than the node itself.

Approach
  • Perform a DFS traversal, threading the maximum value seen so far on the current root-to-node path.
  • At each node, if its value is greater than or equal to the running maximum, it qualifies as good and we increment the count while updating the maximum.
  • Recurse into both children with the (possibly updated) maximum, then return the total count from both subtrees.
  • This works because a node is good exactly when its value dominates every ancestor, which is captured by comparing against the running path maximum.
#include <algorithm>
#include <climits>
using namespace std;

// Definition for a binary tree node.
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int goodNodes(TreeNode* root) {
        return dfs(root, INT_MIN);
    }

private:
    // Returns the count of good nodes in the subtree rooted at `node`,
    // given that `pathMax` is the maximum value on the path from the root to `node`'s parent.
    int dfs(TreeNode* node, int pathMax) {
        if (!node) return 0;

        // A node is "good" if no ancestor has a strictly greater value.
        int isGood = (node->val >= pathMax) ? 1 : 0;
        int newMax  = max(pathMax, node->val);

        return isGood
             + dfs(node->left,  newMax)
             + dfs(node->right, newMax);
    }
};
Time O(n) Space O(h), where h is the tree height (O(log n) balanced, O(n) skewed)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
 
class Solution {
public:
    int goodNodes(TreeNode* root) {
        return dfs(root, root->val);
    }
    int dfs(TreeNode* curNode, int maxValue){
        int count=0;
        if(!curNode)
            return 0;
        if(curNode->val>=maxValue){
            maxValue=curNode->val;
            count++;
        }
        count+=dfs(curNode->left, maxValue);
        count+=dfs(curNode->right, maxValue);

        return count;
    }
};

// Time: O(n)
// Space: O(h) where h is the height of the tree, since we are using recursion (the call stack will take up space). 
// In the worst case (skewed tree), h = n. In the best case (balanced tree), h = log(n).

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern