75-days-dsa-challenge

Ninja technique🥷 to ACE DSA Interviews.

View the Project on GitHub

1448. Count Good Nodes in Binary Tree

Pre-order Traversal

Code

class Solution {
private:
    int ans = 0;
    void preorder(TreeNode* root, int maxVal)
    {
        if (root == NULL) return;
        if (root->val >= maxVal){
            ans++;
            maxVal = root->val;
        }
        preorder(root->left, maxVal);
        preorder(root->right, maxVal);
    }

public:
    int goodNodes(TreeNode* root)
    {
        preorder(root, INT_MIN);
        return ans;
    }
};