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;
}C语言 判断二叉树是不是平衡树
最新推荐文章于 2023-03-19 15:40:47 发布
本文探讨了如何通过实现两个函数来检查并确保平衡二叉树的平衡性,包括获取树的深度和判断树是否为平衡树。重点在于理解平衡二叉树的概念及其在计算机科学中的应用。
6521

被折叠的 条评论
为什么被折叠?



