平衡二叉树

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;
    }
};
posted @ 2023-05-07 17:01  穿过雾的阴霾  阅读(8)  评论(0)    收藏  举报