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

2018-10-13  本文已影响0人  小码弟

如题

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

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);
}
上一篇 下一篇

猜你喜欢

热点阅读