/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root == NULL)
return true;
int differ = height(root->left) - height(root->right);
//cout << differ << endl;
if(differ < -1 || differ > 1)
return false;
else
return isBalanced(root->left) && isBalanced(root->right);
}
int height(TreeNode* root){
if(root == NULL)
return 0;
int dl = height(root->left);
int dr = height(root->right);
return dl>dr? dl+1:dr+1;
}
};
遇见二叉树就是递归!!!不要想其他方法了,好好审题,平衡二叉树:“a binary tree in which the depth of the two subtrees of every node never differ by more than 1.” every node!!!!!