class Solution {
public:
bool IsBalanced_Solution(TreeNode* root) {
if(!root)
return true;
int tem = getheight(root->left) - getheight(root->right);
int ans = (tem<=1&&tem>=-1);
return IsBalanced_Solution(root->left)&&IsBalanced_Solution(root->right)&&ans;
}
int getheight(TreeNode *root){
if(!root)
return 0;
return max(getheight(root->left),getheight(root->right))+1;
}
};
剑指offer 输入一棵二叉树,判断该二叉树是否是平衡二叉树。
最新推荐文章于 2019-09-12 15:33:57 发布