Simple C++ solution


  • 0
    H

    bool helper(TreeNode* root, int& count){

        if(root == NULL) return true;
        
        bool leftRet = helper(root->left,count);
        bool rightRet = helper(root->right,count);
        
        bool retV = false;
        if (leftRet && rightRet){
            if ((root->left == NULL || root->left->val == root->val) && (root->right == NULL || root->right->val == root->val)){
                count++;
                retV = true;
            }
        }
        
        return retV;
        
    }
    
    
    int countUnivalSubtrees(TreeNode* root) {
        if(root == NULL) return 0;
        
        int count = 0;
        
        bool res = helper(root, count);
        
        
        return count;
       
    }

  • 0
    R

    Hi, how do you access to the question? it is locked for me


  • 0
    H

    you need to pay for the premium membership


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.