-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0222.cpp
More file actions
27 lines (26 loc) · 715 Bytes
/
Copy path0222.cpp
File metadata and controls
27 lines (26 loc) · 715 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
int countNodes(TreeNode *root) {
if (!root) return 0;
int height = 0;
queue<TreeNode *> q;
q.push(root);
while (!q.empty()) {
TreeNode *root = q.front();
q.pop();
int rh = 0, lh = 0;
for (TreeNode *t = root; t; t = t->left)
lh++;
for (TreeNode *t = root; t; t = t->right)
rh++;
if (lh == rh)
height += exp2(rh) - 1;
else {
height++;
if (root->left) q.push(root->left);
if (root->right) q.push(root->right);
}
}
return height;
}
};