DEV Community

Tanuja V
Tanuja V

Posted on • Edited on

3 1 1

Count Good Nodes in Binary Tree | LeetCode | Java

Code

class Solution {
    int count = 0;

    public int goodNodes(TreeNode root) {
        countGoodNodes(root, root.val);
        return count;
    }

    void countGoodNodes(TreeNode node, int val){
        if(node==null)
            return;

        if(node.val>=val){
            val = node.val;
            count++;
        }

        countGoodNodes(node.left, val);
        countGoodNodes(node.right, val);
    }
}
Enter fullscreen mode Exit fullscreen mode

Thanks for reading :)
Feel free to comment and like the post if you found it helpful
Follow for more 🤝 && Happy Coding 🚀

If you enjoy my content, support me by following me on my other socials:
https://linktr.ee/tanujav7

Top comments (0)

👋 Kindness is contagious

Dive into this informative piece, backed by our vibrant DEV Community

Whether you’re a novice or a pro, your perspective enriches our collective insight.

A simple “thank you” can lift someone’s spirits—share your gratitude in the comments!

On DEV, the power of shared knowledge paves a smoother path and tightens our community ties. Found value here? A quick thanks to the author makes a big impact.

Okay