Esempio n. 1
0
int recursiveCountWithHeight(struct TreeNode* root,int height) {
    if(NULL==root) return 0;

    struct TreeNode *p=root;
    int r_h=0;
    while(NULL!=p) {
        ++r_h;
        p=p->right;
    }

    if(height==r_h) {
        return pow(2,height)-1;
    } else {
        return 1+recursiveCountWithHeight(root->left,height-1)
               +recursiveCountWithHeight(root->right,height-1);
    }
}
Esempio n. 2
0
    int countNodes(TreeNode* root) {
        if(NULL==root) return 0;

    	TreeNode *p=root;
    	int height=0;
    	// left height only needs to be calculated once
    	while(NULL!=p){
    		++height;
    		p=p->left;
    	}
    
    	return recursiveCountWithHeight(root,height);
    }