Share my code c++ simple and easy understanding


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

Log in to reply
 

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