Skip to content

Commit d148108

Browse files
committed
222
1 parent 73f7b9d commit d148108

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

222.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
class Solution {
3+
public:
4+
int countNodes(TreeNode* root) {
5+
if(root==NULL){
6+
return 0;
7+
}
8+
int left_height=1;
9+
TreeNode * left= root->left;
10+
while(left!=NULL){
11+
left=left->left;
12+
left_height++;
13+
}
14+
int right_height=1;
15+
TreeNode * right= root->right;
16+
while(right!=NULL){
17+
right=right->right;
18+
right_height++;
19+
}
20+
if(right_height==left_height){
21+
return pow(2,right_height) -1 ;
22+
}
23+
24+
return 1 + countNodes(root->left) + countNodes(root->right);
25+
}
26+
};

0 commit comments

Comments
 (0)