class Solution {
public:
int getdep(TreeNode *root){
int dep=0;
while(root) root=root->left,++dep;
return dep;
}
int countNodes(TreeNode* root) {
if(!root) return NULL;
int dep1=getdep(root->left),dep2=getdep(root->right);
if(dep1==dep2) return countNodes(root->right)+(1<<dep1);//说明root->left是满二叉鸭
return countNodes(root->left)+(1<<dep2);//说明root->right是满二叉鸭
}
};
leetcode 222. 完全二叉树的节点个数(复杂度logn*logn 左右子树深度相同 左边满二叉左子树节点个数是1<<lson的dep -1)
最新推荐文章于 2022-05-24 22:03:19 发布