判断一棵树是不是平衡二叉树

如题

平衡二叉树是递归定义的,同样解法也用递归。

bool IsBalanced(BTree root)
{
  if(root == NULL) return true;
 int ldepth = Depth(root->lchild);
 int rdepth = Depth(root->rchild);
 
 if(abs(ldepth-rdepth)>1)return false;
 
  return IsBanlanced(root->lchild)&&IsBanlanced(root->rchild);
}

你可能感兴趣的:(判断一棵树是不是平衡二叉树)