33,028
社区成员
发帖
与我相关
我的任务
分享
typedef struct bitnode
{
char value;
bitnode *left;
bitnode *right;
}*bitree,bitnode;
#include<iostream>
#include<queue>
#include"struct.h"
#define Null 0
using namespace std;
int judge(bitree T)
{
if(T==Null)return 0;
int flag=1;
queue<bitree> q;
bitree p=T;
q.push(p);
while(!q.empty())
{
p=q.front();
q.pop();
if(p==Null)flag=0;
if(p!=Null&&flag==0)return 0;
if(p!=Null)
{
if(p->left==Null&&p->right!=Null)return 0;
q.push(p->left);
q.push(p->right);
}
}
return 1;
}
typedef struct bitnode
{
char value;
bitnode *left;
bitnode *right;
}*bitree,bitnode;
/***********************************************************************
功能:判断以 T 为根节点的二叉树是不是满二叉树
返回:-1不是满二叉树,否则是满二叉树且返回值是二叉树子节点的层数
***********************************************************************/
int judge(bitree T)
{
// 左右节点均为空,是满二叉树
if( NULL == T->left && NULL == T->right)
{
return 0;
}
// 左右节点只有一个为空,不是满二叉树
if( NULL == T->left)
{
return -1;
}
if( NULL == T->right)
{
return -1;
}
int nLeft,nRight;
// 递归判断左右子树是不是满二叉树
nLeft = judge(T->left);
nRight = judge(T->right);
if ( (nLeft == nRight) && (0 <= nLeft) )
{
// 左右子树均为满二叉树,且左右子树的子节点层数相等,该二叉树是满二叉树
// 并返回其子树的层数为左子树的子树层数加一
return (1 + nLeft);
}
else
{
// 不是满二叉树
return -1;
}
}