C++ || 2 Approaches || Easy to understand ||
Runtime: 24 ms, faster than 43.07% of C++ online submissions for Balanced Binary Tree.\nMemory Usage: 21.5 MB, less than 86.00% of C++ online submissions for Ba
***Runtime: 24 ms, faster than 43.07% of C++ online submissions for Balanced Binary Tree.\nMemory Usage: 21.5 MB, less than 86.00% of C++ online submissions for Balanced Binary Tree.***\n```\nclass Solution {\npublic:\n    bool isBalanced(TreeNode* root) {\n        if(root == nullptr)\n            return true;\n        else{\n            int leftH = height(root->left);\n            int rightH = height(root->right);\n            int diff = abs(rightH - leftH);\n            return diff <= 1 && isBalanced(root->left) && isBalanced(root->right);   \n        } \n    }\n    int height(TreeNode* root){\n        if(root == nullptr){\n            return 0;\n        }else{\n            return 1 + max(height(root->left), height(root->right));\n        }\n    }\n};\n```\n\n***Runtime: 12 ms, faster than 98.58% of C++ online submissions for Balanced Binary Tree.\nMemory Usage: 21.9 MB, less than 29.08% of C++ online submissions for Balanced Binary Tree.***\n```\nclass Solution {\npublic:\n    bool isBalanced(TreeNode* root) {\n        if(root == nullptr)\n            return true;\n        return height(root) != -1;\n    }\n    int height(TreeNode* root){\n        if(root == nullptr){\n            return 0;\n        }\n        int lHeight = height(root->left);\n        int rHeight = height(root->right);\n        if(lHeight == -1 || rHeight == -1 || abs(lHeight - rHeight) > 1){\n            return -1;\n        }\n        return 1 + max(lHeight, rHeight);\n    }\n};\n```
['C', 'C++']