class Solution {
public:
bool res=true;
int dfs(TreeNode* root)//返回以root为根节点的子树深度
{
if(root==NULL) return 0;
int l=dfs(root->left),r=dfs(root->right);
if(abs(l-r)>1)
res=false;
return max(l,r)+1;
}
bool isBalanced(TreeNode* root) {
dfs(root);
return res;
}
};
平衡二叉树
发布时间 2023-05-07 17:06:19作者: 穿过雾的阴霾