天天看點

判斷一個樹是不是二叉平衡樹

bool isBalanced(node* n)
{
	try
	{
		checkBalanced(n);
	}
	catch(string& s)
		return false;

	return true;
};

int checkBalanced(node* n)
{
	if(!n)
		return 0;

	int lHeight = checkBalanced(n->left);
	int rHeight = checkBalanced(n->right);

	if(abs(lHeight - rHeight) > 1)
		throw "unbalanced";

	return 1 + max(lHeight, rHeight);
};