# LeetCode 之 Balanced Binary Tree

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

1 ：用递归的方法，假设每次判定到的节点为current

2 ： 判断current的左右子树的高度，如果高度差<=1，则返回max(坐，右)+1；如果高度差大于1，或者左右子树有一个的高度差为-1，则返回-1（KEY），用来记录，说明这棵树已经是非平衡的了。。。

class Solution {
public:
bool isBalanced(TreeNode *root) {
if (!root) return true;
else {
int left = height(root->left);
int right = height(root->right);
if(left==-1||right==-1) return false;
return (left-right)>1 || (left-right)<-1 ? false:true;
}
}
int height (TreeNode * root){
if(!root) return 0;
else if(root->left==NULL && root->right==NULL){
return 1;
}
else {
int left = height(root->left);
int right= height(root->right);
if(left ==-1 || right == -1 || left-right>1 || right-left>1) return -1;
return left>right?left+1:right+1;
}

}
};

#### LeetCode(110)Balanced Binary Tree

2014-01-16 07:46:33

#### 【LeetCode】Balanced Binary Tree 解题报告

2014-10-12 20:15:29

#### 【leetCode】 Balanced Binary Tree python版实现

2016-06-18 16:56:00

#### leetcode的题目：Balanced Binary Tree

2014年10月14日 145KB 下载

#### Leetcode_110_Balanced Binary Tree

2014-12-28 21:35:16

#### 【LeetCode-面试算法经典-Java实现】【110-Balanced Binary Tree（平衡二叉树）】

2015-08-11 07:34:02

#### 【Leetcode】【python】Balanced Binary Tree

2017-08-03 04:10:38

#### LeetCode-Balanced Binary Tree && 二叉树的深度 && 二叉树的平衡

2014-05-20 13:16:47

#### leetcode---Balanced Binary Tree

2016-04-09 18:27:00

#### LeetCode Convert Sorted List to Binary Search Tree 解题报告

2014-10-01 22:30:08