O(n)time/BEATS 99.97% MEMORY/SPEED 0ms MAY 2022
\n\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, please upvote this post.)\nTake care broth
```\n```\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, ***please upvote*** this post.)\n***Take care brother, peace, love!***\n\n```\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 38.2MB*** (beats 92.04% / 24.00%).\n* ***Java***\n```\npublic class Solution {\nprivate boolean result = true;\n\npublic boolean isBalanced(TreeNode root) {\n    maxDepth(root);\n    return result;\n}\n\npublic int maxDepth(TreeNode root) {\n    if (root == null)\n        return 0;\n    int l = maxDepth(root.left);\n    int r = maxDepth(root.right);\n    if (Math.abs(l - r) > 1)\n        result = false;\n    return 1 + Math.max(l, r);\n}\n}\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 7.0MB*** (beats 100.00% / 100.00%).\n* ***C++***\n```\nclass Solution {\npublic:\n    bool ans;\n    int checkBalance(TreeNode* root){\n        if(!root)\n            return 0;\n        if(!ans) // if Answer is already False then return it.\n            return 0;\n        int leftSubTree = checkBalance(root->left);\n        int rightSubTree = checkBalance(root->right);\n        if(abs(leftSubTree-rightSubTree) > 1){\n            ans = false;\n        }\n        return 1 + max(leftSubTree, rightSubTree);\n    }\n    bool isBalanced(TreeNode* root){\n        ans = true;\n        int temp = checkBalance(root);\n        return ans;\n    }\n};\n```\n\n```\n```\n\n```\n```\n\n\nThe best result for the code below is ***26ms / 12.2MB*** (beats 95.42% / 82.32%).\n* ***Python***\n```\nclass Solution(object):\n    def isBalanced(self, root):\n            \n        def check(root):\n            if root is None:\n                return 0\n            left  = check(root.left)\n            right = check(root.right)\n            if left == -1 or right == -1 or abs(left - right) > 1:\n                return -1\n            return 1 + max(left, right)\n            \n        return check(root) != -1\n\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***51ms / 34.2MB*** (beats 100.00% / 84.12%).\n* ***JavaScript***\n```\nvar isBalanced = function(root) {\n    \n    let dfs = function(node) {\n        if (!node) return 0;\n        let left = 1 + dfs(node.left);\n        let right = 1 + dfs(node.right);\n        if (Math.abs(left - right) > 1) return Infinity;\n        return Math.max(left, right);\n    }\n    \n    return dfs(root)==Infinity?false:true;\n};\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***68ms / 44.2MB*** (beats 100.00% / 45.25%).\n* ***Kotlin***\n```\nclass Solution {\n    \n    fun isBalanced(root: TreeNode?): Boolean {\n        return checkDepth(root) != -1\n    }\n    \n    private fun checkDepth(root: TreeNode?): Int {\n        root ?: return 0\n        val lDepth = checkDepth(root.left)\n        val rDepth = checkDepth(root.right)\n        val delta = Math.abs(lDepth - rDepth)\n        return when {\n            lDepth == -1 ||\n            rDepth == -1 ||\n            Math.abs(lDepth - rDepth) > 1 -> -1\n            else -> 1 + maxOf(lDepth, rDepth)\n        }\n    }\n}\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***12ms / 32.2MB*** (beats 95% / 84%).\n* ***Swift***\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     public var val: Int\n *     public var left: TreeNode?\n *     public var right: TreeNode?\n *     public init() { self.val = 0; self.left = nil; self.right = nil; }\n *     public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }\n *     public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {\n *         self.val = val\n *         self.left = left\n *         self.right = right\n *     }\n * }\n */\nclass Solution {\n    // - Complexity:\n    //   - time: O(n), where n is the number of nodes in the binary tree.\n    //   - space: O(n), where n is the number of nodes in the binary tree.\n    \n    func isBalanced(_ root: TreeNode?) -> Bool {\n        isBalancedTree(root).balanced\n    }\n    \n    \n    private func isBalancedTree(_ node: TreeNode?) -> (height: Int, balanced: Bool) {\n        guard let node = node else { return (-1, true) }\n        \n        let left = isBalancedTree(node.left)\n        guard left.balanced else { return(-1, false) }\n        \n        let right = isBalancedTree(node.right)\n        guard right.balanced else { return (-1, false) }\n        \n        guard abs(left.height - right.height) < 2 else { return (-1, false) }\n        \n        return (max(left.height, right.height) + 1, true)\n    }\n\n}\n```\n\n```\n```\n\n```\n```\n\n***"Open your eyes. Expect us." - \uD835\uDCD0\uD835\uDCF7\uD835\uDCF8\uD835\uDCF7\uD835\uDD02\uD835\uDCF6\uD835\uDCF8\uD835\uDCFE\uD835\uDCFC***\n
['Depth-First Search', 'Recursion', 'Swift', 'C', 'Python', 'Java', 'Python3', 'Kotlin', 'JavaScript']