C语言 判断二叉树是不是平衡树

int getTreeDepth(treeNode *pRoot){
	if(pRoot == NULL){
		return 0;
	}
	int left = getTreeDepth(pRoot->pLeft);
	int right = getTreeDepth(pRoot->pRight);
	return left > right ? left+1 : right+1;
}
bool isBalanceTree(treeNode *pRoot){
	if(pRoot == NULL){
		return true;
	}
	int left = getTreeDepth(pRoot->pLeft);
	int right = getTreeDepth(pRoot->pRight);
	int diff = left-right;
	if(diff > 1 || diff < -1){
		return true;
	}
	return isBalanceTree(pRoot->pLeft) && isBalanceTree(pRoot->pRight);
}
bool isBalanceTree01(treeNode *pRoot, int &depth){
	if(pRoot == NULL){
		depth = 0;
		return true;
	}
	int left;
	int right;
	if(isBalanceTree(pRoot->pLeft, &left) && isBalanceTree(pRoot->pRight, &right)){
		int diff = left-right;
		if(diff < 1 && diff > -1){
			depth = left > right ? left+1 : right+1;
			return true;
		}
	}
	return false;
}

    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/bad7code/article/details/46443865
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞