96.60% TypeScript Recursion
Intuition\n Describe your first thoughts on how to solve this problem. \nPanic. I did think that it would be a good case for recursion though and thought that I
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nPanic. I did think that it would be a good case for recursion though and thought that I might need to track depths on individual nodes in a map to then interate over and find it there was an imbalance.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThis uses DFS recursion to get the depths of nodes and compares the right and left children depth to check for an imbalance. I used a refernce variable to pass through the function calls so that it could be set once if we find any imbalance.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n) since every node is visted once.\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n) for the stack but no additional data structures were used. \n# Code\n```\n/**\n * Definition for a binary tree node.\n * class TreeNode {\n *     val: number\n *     left: TreeNode | null\n *     right: TreeNode | null\n *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n *         this.val = (val===undefined ? 0 : val)\n *         this.left = (left===undefined ? null : left)\n *         this.right = (right===undefined ? null : right)\n *     }\n * }\n */\n\nfunction isBalanced(root: TreeNode | null): boolean {\n    let answer = {isBalanced: true};\n    getDepth(root, answer);\n    return answer.isBalanced;\n};\n\nfunction getDepth(root: TreeNode | null, answer: any): number {\n    if(!root) {\n        return 0;\n    }\n    const leftDepth = getDepth(root.left, answer);\n    const rightDepth = getDepth(root.right, answer);\n\n    if(Math.abs(leftDepth - rightDepth) > 1) {\n        answer.isBalanced = false;\n    }\n    return Math.max(leftDepth + 1, rightDepth + 1);\n}\n```
['TypeScript']