leetcode-剑指55-II
同主站110
address
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
// 左子树是,右子树是,左剪右-1,0,1,
// 搞个函数,判断,是的话就返回高度,不是的话就-1
bool isBalanced(struct TreeNode* root){
int judge(struct TreeNode* tree){
if(!tree)
return 0;
int left = judge(tree->left);
int right = judge(tree->right);
if((left<0)||(right<0))
return -1;
int cha = left-right;
if((cha<-1)||(cha>1))
return -1;
if (right>left)
left = right;
return (left+1);
}
int ok = judge(root);
if(ok<0)
return false;
return true;
}