判断二叉树是不是满二叉树
方法1:我们算出树高,算出树的节点个数,因为它有个等比数列求和!
方法2:如果满足下图要求,就是满二叉树!
我们在每一层出队列的时候,都可以达到满二叉树的这一行应该有的节点个数,就继续执行下去。
bool Is_Full(BtNode* ptr)
{
int he=1;
bool res=true;
if(ptr==nullptr) return res;
std::queue<BtNode*> qu;
qu.push(ptr);
while(!qu.empty())
{
int n=qu.size();
if(n!=he)
{
res=false;
break;
}
while(n--)
{
ptr=qu.front(); qu.pop();
if(ptr->leftchild!=nullptr) qu.push(ptr->leftchild);
if(ptr->rightchild!=nullptr) qu.push(ptr->rightchild);
}
he+=he;
}
return res;
}