Count Good Nodes in Binary Tree | LeetCode | Java

RMAG news

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);
}
}

Thanks for reading🥰.
Feel free to comment🖌️ and like the post💓
Follow for more 🤝 && Happy Coding🚀👩‍💻

Don’t forget to check out my other socials😍:
Github
Hashnode
Medium
Twitter(X)

Leave a Reply

Your email address will not be published. Required fields are marked *