C++ 16ms ,need to inprove


  • 0
    1
    class Solution {
    public:
        int getheight(TreeNode* root) {
            if(!root) return 0;
            if(!root->left&&!root->right) return 1;
            return max(getheight(root->left),getheight(root->right))+1;
    
        }
        bool isBalanced(TreeNode* root) {
            if(!root) return true;
            if(!root->left&&!root->right) return true;
            int l=getheight(root->left);
            int r=getheight(root->right);
            if(abs(l-r)>1) return false;
            if(isBalanced(root->left)&&isBalanced(root->right)) return true;
            return false;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.