110. 平衡二叉树
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1
**思路:**求二叉树的深度
二叉树深度
/**
* 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 fun(struct TreeNode* root){
if(root==NULL)return 0;
return max(fun(root->left),fun(root->right))+1;
}
bool isBalanced(TreeNode* root) {
if(root==NULL)return true;
return abs(fun(root->left) - fun(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right);
}
};