Accepted solution using recursion and stack

  • 0
    class Solution {
        stack<int> height;
        bool isBalanced(TreeNode* root) {
            if (!root)
                return true;
            if (!isBalanced(root->left)) return false;
            int leftH =; height.pop();
            if (!isBalanced(root->right)) return false;
            int rightH =; height.pop();
            if (abs(leftH - rightH) <= 1)
                height.push(max(leftH, rightH)+1);
                return true;
            else return false;

Log in to reply

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