LeetCode 110. 平衡二叉树
原题链接
简单
作者:
Value
,
2020-08-17 19:22:23
,
所有人可见
,
阅读 438
/**
* 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 == NULL) return 0;
return max(height(root->left), height(root->right)) + 1;
}
bool isBalanced(TreeNode* root) {
if(root == NULL) return true;
return abs(height(root->left) - height(root->right)) <= 1
&& isBalanced(root->left) && isBalanced(root->right);
}
};