Accepted O(n) solution
We determine recursively the height of the root node but when the recursion is coming upwards we return UNBALANCED instead of the actual height if we know that 
We determine recursively the height of the root node but when the recursion is coming upwards we return UNBALANCED instead of the actual height if we know that the tree is already known to be unbalanced. \n\nWe visit each node just once thus it has linear time complexity.\n\n\n    private static final int UNBALANCED = -99;\n    \n    public boolean isBalanced(TreeNode root) {\n        if (root == null) {\n            return true;\n        }\n        return getHeight(root) != UNBALANCED;\n    }\n    \n    private int getHeight(TreeNode root) {\n        if (root == null) {\n            return -1;\n        }\n        int l = getHeight(root.left);\n        int r = getHeight(root.right);\n        if (l == UNBALANCED || r == UNBALANCED || Math.abs(l-r) > 1) {\n            return UNBALANCED;\n        }\n        return 1 + Math.max(l,r);\n    }
[]