Count Good Nodes in Binary Tree LeetCode solution in C++
1class Solution {2public:3int count = 0;4void sol(TreeNode *root, int gr) {5if (!root) return; // base condition67if (gr <= root->val) { // check point max element increase count8gr = max(gr, root->val);9count++;10}1112if (root->left) sol(root->left, gr);13if (root->right) sol(root->right, gr);14}15int goodNodes(TreeNode *root) {16if (!root->left && !root->right) return 1; // check for if there is one node17int gr = root->val;18sol(root, gr);19return count;20}21};