题目背景
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 left and right subtrees of every node
differ in height by no more than 1.Example 1:
Input: root = [3,9,20,null,null,15,7] Output: true
Example 2:
Input: root = [1,2,2,3,3,null,null,4,4] Output: false
Example 3:Input: root = [] Output: true
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/balanced-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法一
class Solution {
public:
int height(TreeNode* root){
if(root == NULL )
return 0;
else
return max(height(root->left),height(root->right))+1;
}
bool isBalanced(TreeNode* root) {
if(root==NULL)
return true;
else
return abs(height(root->left)-height(root->right))<=1&& isBalanced(root->left)&&isBalanced(root->right);
}
};