class Solution {
public:
int countNodes(TreeNode* root) {
if(!root)
return 0;
int hl = 0, hr = 0;
TreeNode *l = root, *r = root;
while(l){
l = l->left;
++hl;
}
while(r){
r = r->right;
++hr;
}
if(hl == hr){
return pow(2, hl) - 1;
}
return 1 + countNodes(root->left) + countNodes(root->right);
}
};
222. Count Complete Tree Nodes
最新推荐文章于 2022-11-27 14:04:43 发布