Count Complete tree nodes


  • 0
    K
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    int count(struct TreeNode* temp)
    {
        static int chk=0;
            if(temp==NULL)
                return;
            else
            {
                chk++;
                count(temp->left);
                //printf("%d",temp->val);
                count(temp->right);
            }
        return chk;
    }
    int countNodes(struct TreeNode* root) {
        if(root==NULL)
            return 0;
        if((root->left==NULL)&&(root->right==NULL))
            return 1;
        int res=count(root);
        return res;
    }
    

    code is working fine for custom testcases but it is not working while submitting.


Log in to reply
 

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