# LeetCode110. Balanced Binary Tree（二叉树 / 搜索）

## LeetCode110. Balanced Binary Tree（二叉树 / 搜索）

• Easy
• Accepted：298,683
• Submissions：738,371

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.

Example 1:

Given the following tree [3,9,20,null,null,15,7]:

    3
/ \
9  20
/  \
15   7

Return true.

Example 2:

Given the following tree [1,2,2,3,3,null,null,4,4]:

       1
/ \
2   2
/ \
3   3
/ \
4   4

Return false.

### 链接

https://leetcode.com/problems/balanced-binary-tree

### 代码

• Runtime: 16 ms, faster than 99.63% of C++ online submissions for Balanced Binary Tree.
• Memory Usage: 17.2 MB, less than 83.65% of C++ online submissions for Balanced Binary Tree.
class Solution {
public:
bool isBalanced(TreeNode* root) {
solve(root);
return flag;
}

private:
bool flag = true;
int solve(TreeNode* root)
{
if (root == NULL)
return 0;
int leftDepth = solve(root->left);
int rightDepth = solve(root->right);
if (abs(leftDepth - rightDepth) > 1) // 高度差超过1
{
flag = false;
return 0;
}
return max(leftDepth, rightDepth) + 1;
}
};

The end.
2019年3月15日 星期五