题解 | #判断是不是平衡二叉树#
class Solution {
public:
int height(TreeNode *pRoot){
if(pRoot==nullptr){
return 0;
}
int t=1;
int tr=height(pRoot->right);
int tl=height(pRoot->left);
if(tr==-1||tl==-1){
return -1;
}
if(tr-tl>1||tl-tr>1){
return -1;
}
return t+=(tr>tl?tr:tl);
}
bool IsBalanced_Solution(TreeNode* pRoot) {
if(height(pRoot)>=0){
return true;
}
return false;
}
};
查看12道真题和解析