Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Count Complete Tree Nodes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
int findleftheight(TreeNode* root){
int h=0;
while(root!=NULL){
h++;
root=root->left;
}
return h;
}
int findrightheight(TreeNode* root){
int h1=0;
while(root!=NULL){
h1++;
root=root->right;
}
return h1;
}
int countNodes(TreeNode* root) {
if(root==NULL)return 0;
int lh=findleftheight(root);
int rh=findrightheight(root);
if(lh==rh)return (1 << lh)-1;
return 1+countNodes(root->left) + countNodes(root->right);

}
};