My simple c++ solution


  • 3
    K
    class Solution {
    public:
    
    int helper(TreeNode * root)
    {
        if(!root)return 0;
        int leftside=0, rightside=0;
        TreeNode *l=root, *r=root;
        while(l){ 
            leftside++; 
            l=l->left; 
            
        }
        while(r){
            rightside++; r=r->right; 
        }
        if(rightside==leftside) return pow(2, rightside)-1;
        return helper(root->left)+helper(root->right)+1;
    }
    int countNodes(TreeNode* root) {
        return helper(root);
    }
    

    };


Log in to reply
 

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