AcWing 72. 平衡二叉树
原题链接
简单
作者:
我要出去乱说
,
2021-02-23 23:19:38
,
所有人可见
,
阅读 479
/**
* 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 height(TreeNode* root) //计算二叉树深度的模板,背就完了
{
if (!root) return 0;
else return max(height(root->left), height(root->right)) + 1;
}
bool isBalanced(TreeNode* root) {
if (!root) return true;
else return abs(height(root->left) - height(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right);
}
};
牛逼!!