题目
LeetCode - 110. Balanced Binary Tree
题目链接
https://leetcode.com/problems/balanced-binary-tree/
参考博客
解题思路
递归解题。
解题源码
/**
* 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:
int getHeight(TreeNode* root){
if(!root) return 0;
return max(getHeight(root->left), getHeight(root->right)) + 1;
}
bool isBalanced(TreeNode* root) {
return !root ||
(isBalanced(root->left) &&
isBalanced(root->right) &&
abs(getHeight(root->left) - getHeight(root->right)) <= 1);
}
};