平衡二叉树
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;
}
};
有帮助的话可以点个赞,我会很开心的~