题目:
Given a complete binary tree, count the number of nodes.
Example:
Input:
1
/ \
2 3
/ \ /
4 5 6
Output: 6
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int countNodes(TreeNode* root) {
if(root == nullptr)
return 0;
TreeNode* l = root;
TreeNode* r = root;
int hl = 0;
int hr = 0;
while(l)
{
++hl;
l = l->left;
}
while(r)
{
++hr;
r = r->right;
}
if(hl == hr)
return pow(2,hl)-1;
else
return 1 + countNodes(root->left) +countNodes(root->right);
}
};