/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode *root)
{
if(!root)
return true;
return isBalanced(root->left) && isBalanced(root->right)
&& abs(maxDepth(root->left)-maxDepth(root->right))<=1;
}
int maxDepth(TreeNode* root)
{
if(root==NULL)
return 0;
return max(maxDepth(root->left),maxDepth(root->right))+1;
}
};
Balanced Binary Tree
最新推荐文章于 2021-11-10 23:01:59 发布