[剑指Offer] 39.平衡二叉树

时间:2021-09-16 14:50:27

题目描述

输入一棵二叉树,判断该二叉树是否是平衡二叉树。

 class Solution {
public:
int Get_Height(TreeNode* root) {
if(root == NULL){
return ;
}
int LeftHeight = Get_Height(root->left);
int RightHeight = Get_Height(root->right);
return max(LeftHeight, RightHeight) + ;
}
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot == NULL){
return true;
}
if(!IsBalanced_Solution(pRoot->left)){
return false;
}
int LeftHeight = Get_Height(pRoot->left);
if(!IsBalanced_Solution(pRoot->right)){
return false;
}
int RightHeight = Get_Height(pRoot->right);
if(LeftHeight - RightHeight > || LeftHeight - RightHeight < -){
return false;
}
return true;
}
};