> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/110.-balanced-binary-tree-easy.md).

# 110. Balanced Binary Tree

## 1.問題

* 給予一個binary tree, 確定是否屬於高度平衡樹

![](/files/-LIKf2PArbB_c7D3JQ9g)

## 2.想法

* 高度平衡樹的定義
* 利用post-order traversal來比較左右子節點的高度

## 3.程式碼

```
/**
 * 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) {
            return true;
        }
        
        return checkHeight(root) != INT_MIN;
    }
private:
    int checkHeight(TreeNode* root) {
        if (!root) {
            return -1;
        }
        int left = checkHeight(root->left);
        if (left == INT_MIN) {
            return INT_MIN;
        }
        
        int right = checkHeight(root->right);
        if (right == INT_MIN) {
            return INT_MIN;
        }
        
        int diff = left - right;
        if (abs(diff) > 1) {
            return INT_MIN;
        } else {
            return max(left, right) + 1;
        }
    }
};
```

## 4.Performance

![](/files/-LIKgZNb7pAGgX6gvFvE)
